【问题标题】:WPF DataGrid group header: selectable and change color on mouse overWPF DataGrid 组标题:鼠标悬停时可选择并更改颜色
【发布时间】:2021-02-23 20:16:58
【问题描述】:

我有一个 WPF DataGrid 中的项目列表。在组模式下,我想突出显示组标题的颜色,对于拖放,标题应该是可选的。

我认为,我必须根据 Expander 制作一个用户控件,因为 Expander 本身没有 IsSelected 标志。 一开始我尝试更改 Expander 标头的颜色,但我只能对包含组属性文本和项目计数的 StackPanel 执行此操作。

我将 StackPanel 更改为 Grid,以便更好地格式化分组属性和项目计数。

IsMouseDirectlyOverChanged 事件未触发。

                    <DataGrid.GroupStyle>
                        <GroupStyle>
                            <GroupStyle.ContainerStyle>
                                <Style TargetType="{x:Type GroupItem}">
                                    <Setter Property="Margin" Value="0,0,0,5"/>
                                    <Setter Property="Template">
                                        <Setter.Value>
                                            <ControlTemplate TargetType="{x:Type GroupItem}">
                                                <Expander Background="#1E90FF" BorderBrush="Gray" Foreground="Black" BorderThickness="0 0 0 3" IsMouseDirectlyOverChanged="Expander_IsMouseDirectlyOverChanged">
                                                    <Expander.Header>
                                                        <Grid>
                                                            <Grid.ColumnDefinitions>
                                                                <ColumnDefinition Width="200"/>
                                                                <ColumnDefinition Width="*"/>
                                                            </Grid.ColumnDefinitions>
                                                            <TextBlock Grid.Column="0" FontWeight="Bold" Text="{Binding Path=Name}" Margin="5,0,0,0"/>
                                                            <TextBlock Grid.Column="1" FontWeight="Bold" Text="{Binding Path=ItemCount}"/>
                                                        </Grid>
                                                    </Expander.Header>
                                                    <Expander.Content>
                                                        <ItemsPresenter />
                                                    </Expander.Content>
                                                </Expander>
                                            </ControlTemplate>
                                        </Setter.Value>
                                    </Setter>
                                </Style>
                            </GroupStyle.ContainerStyle>
                        </GroupStyle>
                    </DataGrid.GroupStyle>

知道如何管理吗?

【问题讨论】:

    标签: c# wpf datagrid


    【解决方案1】:

    我没有让它与 XAML 一起使用,而是与 Expander MouseEnter 和 MouseLeave 一起使用代码隐藏。 不是很优雅,但很有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-16
      • 1970-01-01
      • 2011-04-14
      相关资源
      最近更新 更多