2012-10-06 42 views
0

我有在XAML定義2列的DataGrid中的獲取DataGrid行

<DataGrid x:Name="marketInfodg" Grid.Row="1" 
ItemsSource = "{Binding ElementName=This, Path=dataTableTest}" 
CanUserAddRows="False"> 
<DataGrid.Columns> 
    <DataGridComboBoxColumn Header="Department Id" x:Name="comboboxColumn1" 
     SelectedValueBinding="{Binding Department Id}" /> 
     <DataGridTemplateColumn x:Name="DataGridTempCol" Header="Selection"> 
      <DataGridTemplateColumn.CellTemplate> 
       <DataTemplate> 
        <ComboBox 
x:     Name = "combo" 
        SelectedValue = "{Binding Selection, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
        ItemsSource = "{Binding comboBoxSelections, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}" 
        DropDownOpened = "combo_DropDownOpened" 
        DisplayMemberPath = "Key" 
        IsEditable="True"> 
        </ComboBox> 
       </DataTemplate> 
      </DataGridTemplateColumn.CellTemplate> 
     </DataGridTemplateColumn> 
    </DataGrid.Columns> 
</DataGrid> 

構造函數的代碼是

_dataTableTest = new DataTable(); 
DataColumn dc = new DataColumn(); 
dc.ReadOnly = false; 
dc.DataType = typeof(String); 
dc.ColumnName = "Department Id"; 
_dataTableTest.Columns.Add(dc); 

DataColumn dc1 = new DataColumn(); 
dc1.ReadOnly = false; 
dc1.DataType = typeof(KeyValuePair<string, double>); 
dc1.ColumnName = "Selection"; 
_dataTableTest.Columns.Add(dc1); 

marketInfodg.ItemsSource = _dataTableTest.DefaultView; 
var row = _dataTableTest.NewRow(); 
row = _dataTableTest.NewRow(); 
_dataTableTest.Rows.Add(row); 
row["Department Id"] = "X567"; 
row["Selection"] = (KeyValuePair<string, double>)comboBoxSelections[0]; 

有效地設置一個單列爲列「部門ID」 =「X567」,第二列是組合框設置爲第一項的組合框選擇[0]

combo_DropDownOpened事件在任何Combobox下拉列表打開時(明顯)第二我可以設置基於發件人變量cb,使用

private void combo_DropDownOpened(object sender, EventArgs e) 
{ 
    var cb = ((System.Windows.Controls.ComboBox)sender); 
} 

我怎麼也得到了射擊ComboBox的相關行(行所有列)和rowIndex位置/數量在combo_DropDownOpened事件?

回答

2

組合框位於visual tree of the DataGrid。如果你旅行了Visual Tree,你會在DataGrid頂部的路上找到DataGridRow。

您可以使用VisualTreeHelper類走到Visual樹。通常,您可以使用此方法在控件的可視化樹中查找任何父項。把這種方法在一些工具類的使用,只要你覺得自己步行到視覺樹控件找到任何父 - 現在

public static Parent FindParent<Parent>(DependencyObject child) 
      where Parent : DependencyObject 
{ 
    DependencyObject parentObject = child; 

    //We are not dealing with Visual, so either we need to fnd parent or 
    //get Visual to get parent from Parent Heirarchy. 
    while (!((parentObject is System.Windows.Media.Visual) 
      || (parentObject is System.Windows.Media.Media3D.Visual3D))) 
    { 
     if (parentObject is Parent || parentObject == null) 
     { 
      return parentObject as Parent; 
     } 
     else 
     { 
      parentObject = (parentObject as FrameworkContentElement).Parent; 
     } 
    } 

    //We have not found parent yet , and we have now visual to work with. 
    parentObject = VisualTreeHelper.GetParent(parentObject); 

    //check if the parent matches the type we're looking for 
    if (parentObject is Parent || parentObject == null) 
    { 
     return parentObject as Parent; 
    } 
    else 
    { 
     //use recursion to proceed with next level 
     return FindParent<Parent>(parentObject); 
    } 
} 

,在下落事件處理程序,你可以用上面的功能找到DataGridRow像這樣 -

private void combo_DropDownOpened(object sender, EventArgs e) 
{ 
    var cb = ((System.Windows.Controls.ComboBox)sender); 
    DataGridRow dataGridRow = FindParent<DataGridRow>(cb); 
    int index = dataGridRow.GetIndex(); 
} 
+0

非常感謝你;這非常有用。 – user3357963

+1

偉大的解決方案的人!謝謝 – Vishal