我有一个在 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”,第二列是设置为 comboBoxSelections[0] 中第一项的组合框
combo_DropDownOpened
当打开任何 Combobox 下拉菜单(显然)时会触发该事件,我可以cb
使用基于发件人设置变量
private void combo_DropDownOpened(object sender, EventArgs e)
{
var cb = ((System.Windows.Controls.ComboBox)sender);
}
如何在combo_DropDownOpened
事件中获取关联的行(行中的所有列)和触发组合框的行索引/编号?