如何设置特定列标题的样式

本文关键字:标题 样式 何设置 设置 | 更新日期: 2023-09-27 18:26:11

我有5列表、扫描计数、逻辑读取等。这是我对所有列的排序处理程序:

<Window.Resources>
    <Style x:Key="GroupHeaderStyle" TargetType="{x:Type GroupItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type GroupItem}">
                    <Expander x:Name="exp" IsExpanded="True"
                      Background="White"
                      Foreground="Black">
                        <Expander.Header>
                            <TextBlock Text="{Binding Name}" />
                        </Expander.Header>
                        <ItemsPresenter />
                    </Expander>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style TargetType="{x:Type DataGridColumnHeader}">
        <EventSetter Event="Click"
                     Handler="GridViewColumnHeaderClickedHandler"/>
    </Style>        
</Window.Resources>
<Grid>
    <DataGrid x:Name="GridView1"                   
      ItemsSource="{Binding listAggregation}"
      CanUserAddRows="False" Margin="0,65,0,46" AllowDrop="True" Drop="Grid_Drop" AutoGenerateColumns="True" >
        <DataGrid.Columns>
            <DataGridTextColumn Header="Table">
                <DataGridTextColumn.HeaderStyle>
                    <Style TargetType="DataGridColumnHeader"
                   BasedOn="{StaticResource {x:Type DataGridColumnHeader}}">
                        <EventSetter Event="PreviewMouseMove" 
                             Handler="DataGridHeader_PreviewMouseMove"/>
                    </Style>
                </DataGridTextColumn.HeaderStyle>
            </DataGridTextColumn>
        </DataGrid.Columns>

        <DataGrid.GroupStyle>
            <GroupStyle ContainerStyle="{StaticResource GroupHeaderStyle}">
                <GroupStyle.Panel>
                    <ItemsPanelTemplate>
                        <DataGridRowsPresenter/>
                    </ItemsPanelTemplate>
                </GroupStyle.Panel>
            </GroupStyle>
        </DataGrid.GroupStyle>
    </DataGrid>
    <Label x:Name="MyGroupDescriptionsList" HorizontalAlignment="Left" Margin="19,10,0,0" VerticalAlignment="Top" Width="151" 
           AllowDrop="True" Drop="Label_Drop" Background="#FFE2D8D8" Height="31"  QueryContinueDrag="Label_QueryContinueDrag" PreviewMouseMove="Label_Mousemove" Content="Drag Your Header Here" FontWeight="Bold"/>
</Grid>

如何将两者合并??我只想让列标题"Table"(它是第一列)能够使用这种拖放方式,并让所有列都使用排序。列是在代码隐藏中创建的。欢迎提出任何建议。谢谢

如何设置特定列标题的样式

您可以使用BasedOn来继承默认样式。但要使其工作,而不是将默认样式显式设置为ColumnHeaderStyle,请在DataGrid资源部分中声明它。之后,您可以使用BasedOn来继承默认样式。

<DataGrid AutoGenerateColumn="False">
    <DataGrid.Resources>
        <Style TargetType="{x:Type DataGridColumnHeader}">
            <EventSetter Event="Click"
                         Handler="GridViewColumnHeaderClickedHandler"/>
        </Style>
    </DataGrid.Resources>
    <DataGrid.Columns>
        <DataGridTextColumn Header="Table">
            <DataGridTextColumn.HeaderStyle>
                <Style TargetType="DataGridColumnHeader"
                       BasedOn="{StaticResource {x:Type DataGridColumnHeader}}">
                    <EventSetter Event="PreviewMouseMove" 
                                 Handler="DataGridHeader_PreviewMouseMove"/>
                </Style>
            </DataGridTextColumn.HeaderStyle>
        </DataGridTextColumn>
        .........
    </DataGrid.Columns>
</DataGrid>