0

XAML で次のように定義された 2 つの列を持つ 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];

これは、単一の行を列 "Department Id" = "X567" として効果的に設定し、2 番目の列は、comboBoxSelections[0] の最初の項目に設定されたコンボボックスです。

コンボボックスのcombo_DropDownOpenedドロップダウンが開かれたときにイベントが発生し (明らかに) cb、送信者に基づいて変数を設定できます。

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

関連する行 (行内のすべての列) と、combo_DropDownOpenedイベントで発火する ComboBox の RowIndex/番号も取得するにはどうすればよいですか?

4

1 に答える 1

4

ComboBoxはにありvisual tree of the DataGridます。Visual Treeを上に移動すると、DataGridの最上部に向かう途中にDataGridRowがあります。

クラスを使用VisualTreeHelperして、ビジュアルツリーまで歩くことができます。通常、このメソッドを使用して、コントロールのビジュアルツリーで任意の親を見つけることができます。このメソッドをいくつかのUtilityクラスに入れて、親を見つけるためのコントロールのためにビジュアルツリーまで歩いて行きたいときはいつでも使用してください-

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);
    }
}

これで、dropDownイベントハンドラーで上記の関数を使用して、次のような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();
}
于 2012-10-07T08:12:39.157 に答える