2
我在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>
TextBlock的风格正在为我的主窗口中定义的TextBlock元素,但它不工作作为DataTemplate中的TextBlock的我的ComboBoxes。为什么?
如果我设置元素本身内部TextBlock的性质,一切工作正常:
<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>