样式不应用正确

问题描述:

这是我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

是它在某种程度上与TextBlockStyle碰撞? 由于FontWeight被应用,似乎有一个连接?!

注:

唯一的 “明显的” 区别我可以看到它,绑定不同:

Day + YearIntegers一个CollectionMonthstring一个Collection?!

+0

天和年组合框不可编辑和月编辑? – nkoniishvt

+0

@nkoniishvt我不使用CodeBehind(只有MVVM),所以我在样式/行为上表达了你在xaml中看到的所有内容! –

这是由于数据的类型和事实,你没有定义的方式来显示数据:ItemTemplate中,ItemTemplateSelector或的StringFormat

如果添加<Setter Property="ItemStringFormat" Value="{}{0}"></Setter>

所有ComboBoxes将正确显示。

ItemsControl.UpdateSelectionBoxItem是负责在选择框中显示数据的函数,但我无法弄清楚在提取和显示Item的过程中,它如何处理与String不同的int。

无论如何,如果我把它作为TextBox和TextBox显示为TextBlocks和String,这就是为什么你int取得你的风格。

+0

解决了它:D谢谢一堆 –

+0

@nk oniishvt真的很有帮助..谢谢 –

也许你可以尝试这样的事:

<Window.Resources> 
    <Style x:Key="CommonStyle" TargetType="FrameworkElement"> 
     <Setter Property="Margin" Value="5" /> 
    </Style> 
    <Style TargetType="ComboBox" BasedOn="{StaticResource CommonStyle}"> 
    </Style>  
</Window.Resources>