Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Style not applied correct

Tags:

c#

styles

wpf

xaml

This is my 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

Is it somehow colliding with the TextBlock Style? Since the FontWeight is applied it seems like there is a connection ?!

NOTE:

The only "obvious" difference I can see it that the Binding differs:

Day + Year is a Collection of Integers while Month is a Collection of string?!

like image 507
Felix D. Avatar asked Jan 21 '26 21:01

Felix D.


1 Answers

This is due to the type of the data and the fact that you didn't define a way to display the data : ItemTemplate, ItemTemplateSelector or StringFormat

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

All ComboBoxes will display correctly.

The ItemsControl.UpdateSelectionBoxItem is the function that is in charge of displaying data in the selection box but I couldn't figure how it treated int differently from String in the process of extracting and displaying the Item.

Anyway, int are displayed as TextBlocks and String as TextBox if I get it right, and that's why you int takes your Style.

like image 130
nkoniishvt Avatar answered Jan 23 '26 09:01

nkoniishvt