c# - 样式应用不正确

标签 c# wpf xaml styles

这是我的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}" />

结果很困惑:

enter image description here

它是否以某种方式与 TextBlock Style 发生冲突? 由于应用了 FontWeight,因此似乎存在关联?!

注意:

我能看到的唯一“明显”区别是绑定(bind)不同:

Day + YearIntegersCollectionMonthCollection 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/

相关文章:

c# - UWP 设置 NavigationView TogglePaneButton 颜色

c# - Control.Invoke 和 Dispatcher.Invoke 之间的区别

wpf - 一个 ViewModel,多个 View

windows-phone-7 - 为什么我不能在不将 child 的背景设置为透明的情况下点击/单击 Border/ContentControl 内的空白区域?

c# mysql sum 查询标签

c# - 如何携带在 Asp.net MVC 中选择的下拉值

c# - (Dis) 连接蓝牙设备与 Windows.Devices.Bluetooth.Rfcomm (WP8.1)

wpf - 与硬件渲染相比,软件渲染导致 CPU 使用率更低

c# - 将文本 block 绑定(bind)到 XAML 中键的字典值?

xaml - 使用 RichtextBlock 格式化文本