2012-08-15 132 views
0

我用三行定義了一個初始表。如果用戶選擇一行並單擊「開始新表」按鈕,它將用新表打開一個新的tabItem。 問題是,我不知道如何選擇我的DataGrid中的整個行。在DataGrid中選擇一行

C# Code: 
//this my initial Table 
private ObservableCollection<TableDataRowStringItem> tableobject = new ObservableCollection<TableDataRowStringItem>(); 
List<TableDataRowStringItem> rowstringList = new List<TableDataRowStringItem>(); 
TableDataRowStringItem item = new TableDataRowStringItem(); 
item.RowNumber = 1; item.saveFlag = true; item.ObjectType = "E"; item.Name = "E"; item.PredecessorRowNumber = "0"; 
rowstringList.Add(item); 
item = new TableDataRowStringItem(); 
item.RowNumber = 2; item.ObjectType = "Function"; item.Name = "Function"; item.PredecessorRowNumber = "1"; 
rowstringList.Add(item); 
item = new TableDataRowStringItem(); 
item.RowNumber = 3; item.ObjectType = "E"; item.Name = "E"; item.PredecessorRowNumber = "2"; 
rowstringList.Add(item); 
rowstringListEPK = rowstringList; 
for (int i = 0; i < rowstringList.Count; i++) 
{ 
    tableobject.Add(rowstringList[i]); 
} 
DataGrid1.ItemsSource = tableobject; 


//Button Code 
foreach (TableDataRowStringItem item in rowstringListEPK) 
{ 
    if (item.ObjectType == "Function" **(&& Hier i schould write if row.Isselected)**) 
    { 
     rowStringItem.Name = item.Name; 
     tabControl.Items.Add(tabItem); 
     tabItem.Focus(); 
     tabItem.IsSelected = true; 
     tabItem.Header = rowStringItem.Name; 
     TableTab.Visibility = Visibility.Visible 
    } 
    else do nothing 
} 

//XAML Code 
<DataGrid.RowStyle> 
    <Style TargetType="DataGridRow"> 
     <Style.Triggers> 
      <Trigger Property="IsSelected" Value="True"> 
       <Setter Property="BorderBrush" Value="Blue" /> 
       <Setter Property="BorderThickness" Value="1" /> 
       <Setter Property="AllowDrop" Value="True" /> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 
</DataGrid.RowStyle> 

回答

0

您的測試將是:

if (dataGrid1.SelectedItem != null) //test if a row is selected 

,你可以訪問數據行與

dataGrid1.SelectedItem 
+0

感謝名單,但我怎麼能寫在喜歡一個例子,如果我選擇行2,按下按鈕,它會打開我的新的TabItem – User1979 2012-08-15 13:22:26

+0

感謝名單,它的工作 – User1979 2012-08-15 13:50:08

+0

喜C#,竭誠爲您服務 – 2012-08-15 13:57:02

1

這可能會幫助:

<DataGrid SelectionMode="Single" SelectionUnit="FullRow" ... 

DataGrid dataGrid = new DataGrid(); 
dataGrid.SelectionUnit = DataGridSelectionUnit.FullRow; 
dataGrid.SelectionMode = DataGridSelectionMode.Single;