【问题标题】:WPF combobox in a datagrid group header数据网格组标题中的 WPF 组合框
【发布时间】:2014-04-24 09:46:17
【问题描述】:

是否可以在数据网格组标题中使用组合框为相应列的每个单元格设置一个选定的值(这个包含组合框)? 或者有没有更好的解决方案同时在一个列中设置多个组合框值?

【问题讨论】:

标签: wpf datagrid combobox


【解决方案1】:

您可以像这样在组标题中添加ComboBox:

<DataGrid.GroupStyle>
    <GroupStyle>
        <GroupStyle.ContainerStyle>
            <Style TargetType="{x:Type GroupItem}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type GroupItem}">
                            <Expander>
                                <Expander.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <TextBlock Text="{Binding Path=Name}" VerticalAlignment="Center" />
                                        <TextBlock Text="{Binding Path=ItemCount}" Margin="5,0,2,0" VerticalAlignment="Center" />
                                        <TextBlock Text="Items" VerticalAlignment="Center" />
                                        <ComboBox Margin="10,0" SelectionChanged="ComboBox_SelectionChanged">
                                            <ComboBoxItem Content="1" />
                                            <ComboBoxItem Content="2" />
                                            <ComboBoxItem Content="3" />
                                        </ComboBox>
                                    </StackPanel>
                                </Expander.Header>
                                <ItemsPresenter />
                            </Expander>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </GroupStyle.ContainerStyle>
    </GroupStyle>
</DataGrid.GroupStyle>

SelectionChanged 事件处理程序如下所示:

private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    DependencyObject dependencyObject = sender as DependencyObject;

    // Walk up the visual tree from the ComboBox to find the GroupItem.
    while (dependencyObject != null && dependencyObject.GetType() != typeof(GroupItem))
    {
        dependencyObject = VisualTreeHelper.GetParent(dependencyObject);
    }

    GroupItem groupItem = dependencyObject as GroupItem;
    CollectionViewGroup collectionViewGroup = groupItem.Content as CollectionViewGroup;

    // Iterate the items of the CollectionViewGroup for the GroupItem.
    foreach (object item in collectionViewGroup.Items)
    {
        // Your logic for changing values here...
    }
}

【讨论】:

  • Eirik,这就是我的想法,非常感谢!关于组合框的另一个问题:如何在数据网格的相应列标题下对齐它?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-05-17
  • 2013-03-19
  • 2018-06-27
  • 2011-08-13
  • 2011-12-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多