в моем DataGrid У меня есть ComboBox в DataGridTemplateColumn, и я хочу реагировать на событие SelectionChanged. Мой XAML:Функция SelectionChanged ComboBox запускается, когда элементы DataGrid сгруппированы
<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>
И SelectionChanged-Метод ComboBox:
private void StatusCB_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// do something
}
DataGrids DataContext является CollectionView из DataTable. Для того, чтобы сгруппировать строки DataGrid я использовать этот код:
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);
Когда новая строка вставляется в DataGrid в SelectionChanged-метод для ComboBox этой строки называется. Когда я изменяю выбранный элемент строк ComboBox, я хочу перегруппировать DataGrid. Но добавление нового GroupDescription в методе regroup вызывает метод SelectionChanged для каждой строки. Это заканчивается бесконечным циклом.
Надеюсь, я мог бы объяснить свою проблему.
Большое спасибо за любую помощь
попробуйте e.Handled = True..in StatusCB_SelectionChanged – Vishal