WPF globally styling a TextBlock inside a DataGrid

点点圈 提交于 2019-12-21 03:46:15

问题


I am encountering a very weird issue. I am trying to apply global styling to several controls within a DataGrid. Most of them work exactly how I would expect them to. However, the styling for the TextBlock never gets applied. Styles for ComboBox, TextBox, Label, and several others all are getting applied to their respective controls, but not the TextBlock. I have simplified the code as much as possible and the issue is still present. I have provided the code sample below.

I need the style to be applied to the TextBlock and I don't want to have to apply it manually to the TextBlock.

<DataGrid ItemsSource="{Binding Data}" AutoGenerateColumns="False">
    <DataGrid.Resources>
        <Style TargetType="TextBlock">
            <Setter Property="ANY_TEXTBLOCK_PROPERTY" Value="VALUE" />
        </Style>
    </DataGrid.Resources>
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="Test">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="Globably Applied" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

More Information:

  • Global styles for any control other than TextBlock (TextBox, ComboBox, etc.) work properly.
  • Defining the global style inside the DataTemplate will work properly.
  • Directly assigning the style to the TextBlock using an x:Key will work.
  • Global styles for DataGridCell using TextElement.PROPERTY will get applied to a TextBlock.

While some of these will get the style applied to the TextBlock, they have there own issues. Directly assigning the style or defining the style somewhere within a DataGridColumn will mean that I will have to apply the style more than once. Using the TextElement.PROPERTY on the DataGridCell will apply the style to more than just TextBlock controls and will limit the number of properties that you can set.


回答1:


So with a bit more digging and a little luck, I discovered that WPF does not apply implicit styles inside templates unless the TargetType derives from Control. Since TextBlock doesn't derive from Control, its style is not applied. So you either have to manually apply the style to every non-Control or define the implicit style inside the template.

The following MSDN blog post explains it in pretty good detail.

http://blogs.msdn.com/b/wpfsdk/archive/2009/08/27/implicit-styles-templates-controls-and-frameworkelements.aspx




回答2:


Unfortunately, like BrianP said, WPF does not work that way. But, it is possible to set the TextElement properties of the cell style as follows:

<DataGrid ItemsSource="{Binding Data}" AutoGenerateColumns="False" DockPanel.Dock="Top">
    <DataGrid.CellStyle>
        <Style TargetType="DataGridCell">
            <Setter Property="TextElement.Foreground" Value="Green" />
        </Style>
    </DataGrid.CellStyle>
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="Test">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="not globably applied" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>


来源:https://stackoverflow.com/questions/12714982/wpf-globally-styling-a-textblock-inside-a-datagrid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!