The style is not applied correctly

This is mine 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}" />

And the result is very confusing:

enter image description here

Somehow he is faced with TextBlock Style? Since applied FontWeight, it looks like a connection exists ?!

NOTE.

The only "obvious" difference I see is that the binding is different:

Day + Yearis Collectionof Integers, but Monthis Collectionof string?!

+4
source share
2 answers

This is due to the data type and the fact that you have not defined how the data is displayed: ItemTemplate, ItemTemplateSelector or StringFormat

If you add <Setter Property="ItemStringFormat" Value="{}{0}"></Setter>

All ComboBoxes will display correctly.

ItemsControl.UpdateSelectionBoxItem - , , , int , String Item.

, int TextBlocks String TextBox, , int .

+2

, - :

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

All Articles