Question

in my DataGrid i have a ComboBox in a DataGridTemplateColumn and i want to react on a SelectionChanged-event. My XAML:

<DataGrid ItemsSource="{Binding}" SelectionChanged="dataGrid1_SelectionChanged" 
    SelectionMode="Single" Name="dataGrid1" ...>
    <DataGrid.GroupStyle>
        <GroupStyle>
            <GroupStyle.ContainerStyle>
                <Style TargetType="{x:Type GroupItem}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type GroupItem}">
                                <Expander Name="exp" IsExpanded="True">
                                    <Expander.Header>
                                        <TextBlock Text="{Binding SelectedValue, 
                                            ElementName=groupCB}"/>
                                    </Expander.Header>
                                    <Expander.Content>
                                        <ItemsPresenter/>
                                    </Expander.Content>
                                </Expander>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </GroupStyle.ContainerStyle>
        </GroupStyle>
    </DataGrid.GroupStyle>

    <DataGrid.Columns>
        <DataGridTemplateColumn Header="status" ...>
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <ComboBox ItemsSource="{Binding Path=StatusL, RelativeSource={
                        RelativeSource Mode=FindAncestor, AncestorType={
                        x:Type Window}}}" SelectedItem="{Binding Status}" 
                        Name="statusCB" SelectionChanged="StatusCB_SelectionChanged" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        ...
    </DataGrid.Columns>
</DataGrid>

And the SelectionChanged-Method of the ComboBox:

private void StatusCB_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    // do something
}

The DataGrids DataContext is a CollectionView of a DataTable. To group the DataGrid rows i use this code:

DataTable _record_data = new DataTable("records");
CollectionView _records_view = (CollectionView)CollectionViewSource.GetDefaultView(_record_data);
dataGrid1.DataContext = _records_view;

PropertyGroupDescription _group_description = new PropertyGroupDescription(groupCB.SelectedValue.ToString()); // groupCB is another ComboBox

_records_view.GroupDescriptions.Clear();
_records_view.GroupDescriptions.Add(_group_description);

When a new row is inserted into the DataGrid the SelectionChanged-method for the ComboBox of that row is called. When i change the selected item of the rows ComboBox i want to regroup the DataGrid. But adding a new GroupDescription in the regroup-method calls the SelectionChanged method of each row. This ends in an infinit loop.

I hope i could explain my problem.

Thanks alot for any help

Was it helpful?

Solution

You can change your event handler to handle DropDownClosed instead of SelectionChanged

<ComboBox DropDownClosed="ComboBox_DropDownClosed" ... />

private void ComboBox_DropDownClosed(object sender, EventArgs e)
{
    //do something
}

This will only execute when the user closes the drop down popup. Note that DropDownClosed will execute if they select the same item or index, unlike SelectionChanged which only executes upon "change"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top