2

我有两个ComboBoxes。一个绑定到枚举值列表,而另一个绑定到自定义类对象列表并具有DisplayMemberPath属性集。

对枚举值的ComboBox绑定应用了隐式TextBlock样式,而ComboBox使用该DisplayMemberPath属性的 则没有。

使用Snoop我可以验证两者ComboBoxes都使用完全相同的控件集(a<ContentPresenter>包含 a <TextBlock>)呈现,但是没有集合中的 a 包含TextBlock5而带有set 的控件不包含。ComboBoxDisplayMemberPathMarginDisplayMemberPath

<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" />

截屏

为什么是这样?我能做些什么来阻止枚举ComboBox继承隐式TextBlock样式?

4

1 回答 1

2

我的假设是DisplayMemberPath创建 aDataTemplate和样式不会在其范围内应用。

尝试设置DisplayMemberPath="."第一次ComboBox使用DataTemplatecontains <TextBlock Text="{Binding .}">,这将阻止应用隐式样式。

于 2012-08-30T19:48:04.837 回答