【发布时间】:2012-08-30 19:36:53
【问题描述】:
我有两个ComboBoxes。一个绑定到枚举值列表,而另一个绑定到自定义类对象列表并具有DisplayMemberPath 属性集。
绑定到枚举值的ComboBox 应用隐式TextBlock 样式,而使用DisplayMemberPath 属性的ComboBox 则没有。
使用Snoop,我可以验证ComboBoxes 是使用完全相同的一组控件呈现的(<ContentPresenter> 包含<TextBlock>),但是ComboBox 中的TextBlock 没有@987654335 @ set 包含一个为 5 的 Margin,而带有 DisplayMemberPath set 的那个则没有。
<Grid.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Margin" Value="5" />
</Style>
</Grid.Resources>
<ComboBox Grid.Row="0" Grid.Column="1"
ItemsSource="{Binding EnumCollection}"
SelectedItem="{Binding SelectedEnum}" />
<ComboBox Grid.Column="1" Grid.Row="2"
ItemsSource="{Binding SomeCollection}"
SelectedItem="{Binding SelectedItem}"
DisplayMemberPath="Name" />
这是为什么?我该怎么做才能阻止 Enum ComboBox 继承隐式 TextBlock 样式?
【问题讨论】:
-
我的假设是
DisplayMemberPath创建了一个DataTemplate,样式不会在其范围内应用。 -
@H.B.它确实...每MSDN“此属性是定义描述如何显示数据对象的默认模板的简单方法”。我知道我可以用
<ComboBox.Resources>做一些令人讨厌的丑陋解决方法,但我希望有一个更清洁的解决方案。 -
@KDiTraglia 这将使
ComboBoxes都继承隐含的TextBlock样式,而我想要相反:让他们两个不继承它。
标签: wpf styles implicit-style