如果我在 DataGridTemplateColumn 中定义一个组合框,如下所示,那么添加的每个新行都会有多个组合框
<DataGridTemplateColumn x:Name="DataGridTempCol" Header="Items">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox x:Name="combo" DisplayMemberPath="Value" SelectedValuePath="Key" ItemsSource="{Binding comboBoxSelections, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}" SelectionChanged="combo_SelectionChanged" >
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
我现在想为每个 Combox 添加 SelectionChanged 事件。我试过以下
private void combo_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// Every SelectedItem is one data object in Dictionary collection.
KeyValuePair<string, string> selob =
(KeyValuePair<string, string>)combo.SelectedItem;
string selKey = selob.Key;
string selvalue = selob.Value;
}
但combo
无法识别,因为它不是 ComboBox 的单个实例。如何引用生成事件的 ComboBox 并访问 SelectedItem?