0

以下のように 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; 
}

ComboBox の単一のインスタンスでcomboはないため、認識されません。イベントを生成した ComboBox を参照して SelectedItem にアクセスするにはどうすればよいですか?

4

1 に答える 1

2

次のことを試しましたか?私の最初の推測では、これにより ComboBox または DataGrid 全体が得られるはずです。

e.OriginalSource
于 2012-10-05T14:33:49.070 に答える