【问题标题】:ComboBox's SelectionChanged is fired when DataGrid elements are grouped当对 DataGrid 元素进行分组时,会触发 ComboBox 的 SelectionChanged
【发布时间】:2013-08-07 08:50:41
【问题描述】:

在我的 DataGrid 中,我在 DataGridTemplateColumn 中有一个 ComboBox,我想对 SelectionChanged 事件做出反应。我的 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。但是在重组方法中添加一个新的 GroupDescription 会调用每一行的 SelectionChanged 方法。这以无限循环结束。

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

非常感谢您的帮助

【问题讨论】:

  • 试试 e.Handled=True..in StatusCB_SelectionChanged

标签: wpf datagrid combobox grouping selectionchanged


【解决方案1】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 2012-02-03
    • 1970-01-01
    • 2016-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-21
    • 2021-03-17
    • 2014-03-26
    相关资源
    最近更新 更多