簡體   English   中英

WPF DataGrid 組 header:鼠標懸停時可選擇和更改顏色

[英]WPF DataGrid group header: selectable and change color on mouse over

我有一個 WPF DataGrid 中的項目列表。 在組模式下,我想突出顯示組 header 的顏色,對於拖放,header 應該是可選的。

我想,我必須根據 Expander 制作一個用戶控件,因為 Expander 本身沒有 IsSelected 標志。 一開始我嘗試更改擴展器 header 的顏色,但我只能對包含組屬性文本和項目計數的 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>

知道如何管理嗎?

我沒有讓它與 XAML 一起使用,而是與 Expander MouseEnter 和 MouseLeave 一起使用代碼隱藏。 不是很優雅,但它有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM