这是我的Xaml
:
<Style TargetType="ComboBox">
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="Foreground" Value="Black" />
<Setter Property="Margin" Value="5" />
</Style>
<Style TargetType="TextBlock">
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="Margin" Value="5" />
<Setter Property="FontSize" Value="20" />
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="Foreground" Value="White" />
</Style>
<Style TargetType="TextBox">
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="Margin" Value="5" />
<Setter Property="Height" Value="35" />
<Setter Property="FontSize" Value="20" />
</Style>
[...]
<ComboBox SelectedIndex="{Binding Path=BirthdayDay, UpdateSourceTrigger=PropertyChanged, FallbackValue=0}" ItemsSource="{Binding Path=Days, UpdateSourceTrigger=PropertyChanged}" />
<ComboBox SelectedIndex="{Binding Path=BirthdayMonth, UpdateSourceTrigger=PropertyChanged, FallbackValue=0}" ItemsSource="{Binding Path=Months, UpdateSourceTrigger=PropertyChanged}" />
<ComboBox SelectedIndex="{Binding Path=BirthdayYear, UpdateSourceTrigger=PropertyChanged, FallbackValue=0}" ItemsSource="{Binding Path=Years, UpdateSourceTrigger=PropertyChanged}" />
结果很困惑:
它是否以某种方式与 TextBlock
Style
发生冲突?
由于应用了 FontWeight
,因此似乎存在关联?!
注意:
我能看到的唯一“明显”区别是绑定(bind)不同:
Day + Year
是 Integers
的 Collection
而 Month
是 Collection
string
?!
最佳答案
这是由于数据的类型以及您没有定义显示数据的方式这一事实:ItemTemplate、ItemTemplateSelector 或 StringFormat
如果添加 <Setter Property="ItemStringFormat" Value="{}{0}"></Setter>
所有组合框都将正确显示。
ItemsControl.UpdateSelectionBoxItem是负责在选择框中显示数据的函数,但我无法理解在提取和显示 Item 的过程中它与 String 有何不同。
无论如何,如果我做对了,int 显示为 TextBlocks,String 显示为 TextBox,这就是为什么 int 采用您的 Style。
关于c# - 样式应用不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37698849/