2

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> 

2 Answers 2

2

Templates have a different scope of sorts, you can move the style to the Application.Resources which apply even within data and control templates across the whole application.

Sign up to request clarification or add additional context in comments.

Comments

0

use dynamic resource

 <DataTemplate DataType="{x:Type local:DataSource}"> <TextBox Style="{DynamicResource TextBoxStyle}" Text="{Binding}" /> </DataTemplate> <ComboBox> <ComboBox.Resources> <Style x:Key="TextBoxStyle" BasedOn="{StaticResource {x:Type ListBoxItem}}" TargetType="TextBox"> </Style> </ComboBox.Resources> </ComboBox> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.