2015-10-14 99 views
0

我在製作一個WPF應用程序。我有一個靜態項目的數據網格。現在我想單擊一行顯示一個新窗口。如果點擊datagrid中的行然後打開新窗口

我在做什麼錯?

這是我的第二個窗口,我想打開點擊時:WindowMail.cs

using System; 

namespace Phish_Finder 
{ 
    internal class WindowMail 
    { 
     internal void Show() 
     { 
      WindowMail wm = new WindowMail(); 
      wm.Show(); 
     } 
    } 
} 

這是我的第一個窗口Mainwindow.xaml.cs

private void DataGrid_MouseDoubleClick(object sender, RoutedEventArgs e) 
    { 
     var currentRowIndex = URLGRID.Items.IndexOf(URLGRID.CurrentItem); 
     { 
      if (URLGRID.CurrentItem != null) 
      { 
       WindowMail wm = new WindowMail(); 
       wm.Show(); 
      } 
     } 
    } 

的方法,這是我的DataGrid

DataGrid x:Name="URLGRID" HorizontalAlignment="Left" Height="400" 
Margin="60,300,0,0" VerticalAlignment="Top" Width="1350" Loaded="DataGrid_Loaded" 
MouseDoubleClick="DataGrid_MouseDoubleClick" 

我是WPF新手,我想我正在混淆我在哪裏應該放置方法。但我不確定。

+0

請問你的代碼的工作? –

+1

您在show函數中調用show函數,看起來像是一個無限循環 – Schuere

回答

0

也許代碼

internal class WindowMail 
{ 
    internal void Show() 
    { 
     WindowMail wm = new WindowMail(); 
     wm.Show(); 
    } 
} 

需要變化不大。我在這裏相信WindowMail類實際上是WindowMail.xaml文件之後的cs類。

在這裏,你需要將

 WindowMail wm = new WindowMail(); 
     wm.Show(); 

線變成

this.Show(); //Standard function of a window 

否則你的代碼不斷重複無限循環......你需要改變

而且,事情:我相信功能Show已經存在於窗口級別。所以重命名它,就像下面一樣,或者重寫一下這個函數。

public void OpenDialog(bool asDialog) 
{ 
    if(asDialog) 
     this.ShowDialog(); 
    else 
     this.Show(); 
} 

然後調用你的函數:

private void DataGrid_MouseDoubleClick(object sender, RoutedEventArgs e) 
{   
    if (URLGRID.SelectedItem!= null) 
    { 
     WindowMail wm = new WindowMail(); 
     wm.OpenDialog(true); 
    }   
} 
0

使用下面的代碼

private void DataGrid_MouseDoubleClick(object sender, RoutedEventArgs e) 
{ 
    var currentRowIndex = URLGRID.Items.IndexOf(URLGRID.selectedItem); 
    { 
     if (URLGRID.selectedItem != null) 
     { 
      WindowMail wm = new WindowMail(); 
      wm.Show(); 
     } 

    } 
} 
相關問題