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

时间:2014-10-11 10:12:52

标签: c# wpf xaml sorting datagrid

我有5列表,ScanCount,逻辑读取等。 这是我对所有列的排序处理程序:

<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'(这是第一列)能够使用此拖放并使所有列都使用排序。列在后面的代码中创建。 欢迎任何建议。谢谢!

1 个答案:

答案 0 :(得分:2)

您可以使用 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>