我在 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 样式适用于在我的 MainWindow 中定义的 TextBlock 元素,但不适用于用作我的 ComboBox 的 DataTemplate 的 TextBlock。为什么?
如果我在元素本身内设置 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>