2016-06-08 69 views
4

這是我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中看到的所有內容! –

回答

2

這是由於數據的類型和事實,你沒有定義的方式來顯示數據: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真的很有幫助..謝謝 –

-1

也許你可以嘗試這樣的事:

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