简体   繁体   中英

Access control which fired the SelectionChanged Event

If I define a Combobox within a DataGridTemplateColumn as below then there will be multiple Comboboxes for each new row added

<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>

I now want to add SelectionChanged event for every Combox. I've tried the following

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

but combo is not recognised as it isn't a single instance of a ComboBox. How do I refer to the ComboBox that generated the event and access the SelectedItem?

Have you tried the following? My first guess is that this should either give you the ComboBox or the entire DataGrid.

e.OriginalSource

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM