2

The style for TextBlock (below) has no effect on the DataTemplate's TextBlock. If I change TextBlock to TextBox in both the style and template, the style applies as I would expect. Why does TextBlock ignore the style?

Thank you,
Ben

<UserControl x:Class="SilverlightApplication1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:l="clr-namespace:SilverlightApplication1" mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
    <UserControl.Resources>
        <Style TargetType="TextBlock">
            <Setter Property="FontSize" Value="20" />
        </Style>
        <Style TargetType="TextBox">
            <Setter Property="FontSize" Value="20" />
        </Style>
        <DataTemplate DataType="l:MyObject">
            <TextBlock Text="{Binding Name}" />
        </DataTemplate>
    </UserControl.Resources>

    <StackPanel>
        <ItemsControl>
            <ItemsControl.Items>
            <l:MyObject Name="Frank" />
            </ItemsControl.Items>
        </ItemsControl>
    </StackPanel>
</UserControl>
Ben Gribaudo
  • 5,057
  • 1
  • 40
  • 75
  • In WPF, this is because `TextBox` derives from `Control`, but `TextBlock` doesn't, see: https://stackoverflow.com/questions/2476305/wpf-some-styles-not-applied-on-datatemplate-controls. I won't mark this as a duplicate, though, because I don't have an autoritative source w.r.t. Silverlight. – Heinzi Jun 02 '20 at 09:14

0 Answers0