Reputation: 15
I don't understand why an implicit TextBlock-Style is not applied in a DataGrid, unless defined in the App.xaml code. IMO it foiles the rule: the nearer to the possible consumer in the VisualTree, the rather applied. By the way it makes no difference whether defined in the DataGrid-Resources, the Window-Resources or both (as in the code below). For convenience I added a style for a Label which shows the result as I expect.
So, running the code like this:
<Window x:Class="DasDataGrid.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="DataGrid Implicit Styles"
Width="300" SizeToContent="Height"
FontSize="14"
WindowStartupLocation="CenterScreen">
<Window.Resources>
<x:Array x:Key="champions" Type="{x:Type sys:String}">
<sys:String>Jim Clark</sys:String>
<sys:String>Jochen Rindt</sys:String>
</x:Array>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="Red"/>
</Style>
</Window.Resources>
<DataGrid ItemsSource="{StaticResource champions}" AutoGenerateColumns="False" MinRowHeight="35">
<DataGrid.Resources>
<ResourceDictionary>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="Red"/>
</Style>
<Style TargetType="Label">
<Setter Property="Foreground" Value="Red"/>
</Style>
</ResourceDictionary>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTemplateColumn Header="Champion (TextBlock)" Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Champion (Label)" Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Label Content="{Binding}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
you get this window:
but adding the style in question to the App.xaml like this:
<Application x:Class="DasDataGrid.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="Red"/>
</Style>
</Application.Resources>
results in this Window:
Upvotes: 0
Views: 32