gpt4 book ai didi

wpf - 当 DataGrid 元素被分组时,ComboBox 的 SelectionChanged 被触发

转载 作者:行者123 更新时间:2023-12-04 04:46:35 24 4
gpt4 key购买 nike

在我的 DataGrid 中,我在 DataGridTemplateColumn 中有一个 ComboBox,我想对 SelectionChanged 事件使用react。我的 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>

以及 ComboBox 的 SelectionChanged-Method:

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

DataGrids DataContext 是 DataTable 的 CollectionView。要将 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 中时,该行的 ComboBox 的 SelectionChanged 方法被调用。当我更改行 ComboBox 的选定项目时,我想重新组合 DataGrid。但是在 regroup-method 中添加一个新的 GroupDescription 会调用每一行的 SelectionChanged 方法。这以无限循环结束。

我希望我能解释我的问题。

非常感谢您的帮助

最佳答案

您可以更改您的事件处理程序来处理 DropDownClosed而不是 SelectionChanged

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

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

这只会在用户关闭下拉弹出窗口时执行。请注意 DropDownClosedSelectionChanged 不同,如果他们选择相同的项目或索引,将执行仅在“更改”时执行

关于wpf - 当 DataGrid 元素被分组时,ComboBox 的 SelectionChanged 被触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18078634/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com