Styling the Xceed ColumnManagerCell

2019-04-17 02:52发布

问题:

I'm interested in styling the column headers on an Xceed DataGrid. The goal is to make the background color grey, with a dark grey border around each header column cell. It seemed to me like the best way to do this was to style the ColumnManager:

<Style TargetType="{x:Type xcdg:ColumnManagerCell}">
    <Setter Property="Template" Value="{StaticResource ColumnManagerCellTemplate}"/>
    <Setter Property="BorderBrush" Value="#c5c5c5"/>
    <Setter Property="BorderThickness" Value="1,1,1,1"/>
</Style>  

Using this template:

    <ControlTemplate x:Key="ColumnManagerCellTemplate" TargetType="xcdg:ColumnManagerCell">
        <Grid Background="LightGray" >
            <xcdg:DataCell Content="{TemplateBinding Content}"
                           HorizontalAlignment="Stretch"
                           VerticalAlignment="Center"
                           Background="LightGray"
                           HorizontalContentAlignment="Left"
                           VerticalContentAlignment="Center"
                           BorderBrush="DarkGray"
                           BorderThickness="2"/>
        </Grid>
    </ControlTemplate>

The background color shows up correctly, as does the content, but I cannot get a dark grey border to show up around each cell. (Or any color border at all.) What am I missing? Shouldn't the BorderBrush and BorderThickness properties control this? They seem to work on the rest of the cells in the grid, but not the ColumnManagerCells.

回答1:

You should use a border instead of the grid and then hook up the template bindings for the border like this:

<Style TargetType="{x:Type xcdg:ColumnManagerCell}">
    <Setter Property="Background" Value="LightGray" />
    <Setter Property="BorderBrush" Value="#c5c5c5"/>
    <Setter Property="BorderThickness" Value="1,1,1,1"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ContentControl">
                <Border Background="{TemplateBinding Background}"
                    BorderBrush="{TemplateBinding BorderBrush}"
                    BorderThickness="{TemplateBinding BorderThickness}">
                    <ContentPresenter Content="{TemplateBinding ContentControl.Content}"
                                    ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}"
                                    ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}" />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

I should mention that my default ControlTemplate for ColumnManagerCell is a ContentPresenter instead of DataCell like below:

<xcdg:DataCell Content="{TemplateBinding Content}" />

Are you sure your using the correct control template?