wpf 在 DataGridTextColumn 中为 TextBlock 创建样式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20352231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 00:01:11  来源:igfitidea点击:

Create style for TextBlock in DataGridTextColumn

wpfdatagridstylesdatagridtextcolumn

提问by timmkrause

I want to create a global style that sets the VerticalAlignmentto Centerfor all TextBlockcontrols inside a DataGridor inside a DataGridTextColumn.

我想创建一个全局样式,为a或 a 中的所有控件设置VerticalAlignmentto 。CenterTextBlockDataGridDataGridTextColumn

I don't want to copy the following into every DataGridTextColumnbecause it feels repetitive.

我不想将以下内容复制到 each 中,DataGridTextColumn因为感觉重复。

<DataGridTextColumn Header="Some Property" Binding="{Binding SomeProperty}">
    <DataGridTextColumn.ElementStyle>
        <Style TargetType="TextBlock">
            <Setter Property="VerticalAlignment" Value="Center"></Setter>
        </Style>
    </DataGridTextColumn.ElementStyle>
</DataGridTextColumn>

I tried something like the following but it doesn't work because DataGridTextColumndoes not inherit from FrameworkElementor FrameworkContentElement. DataGriditself does but any further wrapping I try leads to errors:

我尝试了类似以下的内容,但它不起作用,因为DataGridTextColumn它不是从FrameworkElement或继承的FrameworkContentElementDataGrid本身确实如此,但我尝试的任何进一步包装都会导致错误:

<Style TargetType="DataGridTextColumn">
    <Setter Property="ElementStyle">
        <Setter.Value>
            <Style TargetType="TextBlock">
                <Setter Property="VerticalAlignment" Value="Center"/>
            </Style>
        </Setter.Value>
    </Setter>
</Style>

采纳答案by Gildor

You can define a CellStyleas below:

您可以定义 aCellStyle如下:

<Style x:Key="DataGridCellStyle" TargetType="DataGridCell">
    <Setter Property="FocusVisualStyle" Value="{x:Null}" />
    <Setter Property="BorderThickness" Value="0"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridCell}">
                <Grid Background="{TemplateBinding Background}">
                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

And assign it to the DataGrid: CellStyle="{StaticResource DataGridCellStyle}". In this way all your cells will have content centered.

并将其分配给 DataGrid: CellStyle="{StaticResource DataGridCellStyle}"。通过这种方式,您的所有单元格都将以内容为中心。

EDIT: The above code is from one of my projects and also contains the code to remove grid lines in the DataGrid. You can get them back by changing Gridto Borderin the template. Like this:

编辑:上面的代码来自我的一个项目,还包含删除 DataGrid 中网格线的代码。您可以通过在模板中更改Grid为来取回它们Border。像这样:

<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="{x:Type DataGridCell}">
            <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
            </Border>
        </ControlTemplate>
    </Setter.Value>
</Setter>

回答by Michael Spranger

Create a style as a static resource

将样式创建为静态资源

<UserControl.Resources>
    <Style x:Key="verticalCenter" TargetType="{x:Type TextBlock}">
        <Setter Property="VerticalAlignment" Value="Center" />
    </Style>
</UserControl.Resources>

Then you can assign it to the ElementStyle of the DataGridTextColumn

然后就可以赋值给DataGridTextColumn的ElementStyle

<DataGridTextColumn ElementStyle="{StaticResource verticalCenter}" />

回答by Dwsgg

Just use the DataGridTemplateColumn:

只需使用DataGridTemplateColumn

<DataGridTemplateColumn Width="SizeToCells">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock HorizontalAlignment="Center" Width="100" Height="20"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>