Skip to main content
edited title
Link
brunnerh
  • 188k
  • 30
  • 362
  • 434

WPF/XAML Global Style Not Working

Source Link
Tommaso Belluzzo
  • 23.7k
  • 8
  • 77
  • 102

WPF/XAML Global Style Not Working

I have the following styles defined in my MainWindow.Resources:

<Style TargetType="{x:Type ComboBox}"> <Setter Property="Height" Value="26"/> <Setter Property="ItemTemplate"> <Setter.Value> <DataTemplate> <TextBlock Text="{Binding}"/> </DataTemplate> </Setter.Value> </Setter> <Setter Property="VerticalContentAlignment" Value="Center"/> <Setter Property="Width" Value="358"/> </Style> <Style TargetType="{x:Type TextBlock}"> <Setter Property="MaxWidth" Value="350"/> <Setter Property="TextTrimming" Value="CharacterEllipsis"/> <Setter Property="VerticalAlignment" Value="Center"/> </Style> 

The TextBlock style is working for TextBlock elements defined inside my MainWindow, but it's not working for the TextBlock used as DataTemplate for my ComboBoxes. Why?

If I set the TextBlock properties inside the element itself, everything works fine:

<Style TargetType="{x:Type ComboBox}"> <Setter Property="Height" Value="26"/> <Setter Property="ItemTemplate"> <Setter.Value> <DataTemplate> <TextBlock MaxWidth="350" Text="{Binding}" TextTrimming="CharacterEllipsis" VerticalAlignment="Center"/> </DataTemplate> </Setter.Value> </Setter> <Setter Property="VerticalContentAlignment" Value="Center"/> <Setter Property="Width" Value="358"/> </Style>