2013-11-21 21 views
1

我目前在樣式ComboBox看起來像一個在Visual Studio(與顏色主題一起)。我完成了大部分樣式,但在ContentPresenter處停止顯示當前選擇的對象。如何在ComboBox中設置ContentPresenter的風格?

這件作品的XAML如下所示:

<ContentPresenter Margin="2" IsHitTestVisible="False" VerticalAlignment="Center" HorizontalAlignment="Stretch" 
        Name="ContentSite" 
        ContentTemplate="{TemplateBinding ComboBox.SelectionBoxItemTemplate}" 
        Content="{TemplateBinding ComboBox.SelectionBoxItem}" /> 

的問題是,默認SelectionBoxItemTemplate似乎忽略組合框的前景值:

<ComboBox Margin="4" SelectedIndex="0" Foreground="Red"> 
    <ComboBoxItem>First</ComboBoxItem> 
    <ComboBoxItem>Second</ComboBoxItem> 
    <ComboBoxItem>Third</ComboBoxItem>     
</ComboBox> 

enter image description here

第一個是不可編輯的,SelectionBoxItemTemplate啓動並強制將文本的顏色設置爲黑色(即使手動將設置爲紅色,並將樣式設置爲另一種非黑色顏色)。

如何將當前前景色傳遞給ContentPresenter?如果我不能,我在哪裏可以找到工作替代SelectionBoxItemTemplate? (對於ComboBox中的所有物品類型,其工作正常)

回答

0

我不認爲它會忽略Foreground顏色。如果添加了(正常)ComboBox控制像下面這樣你的XAML ...:

<ComboBox Foreground="Red"> 
    <ComboBoxItem>One</ComboBoxItem> 
    <ComboBoxItem>Two</ComboBoxItem> 
    <ComboBoxItem>Three</ComboBoxItem> 
    <ComboBoxItem>Four</ComboBoxItem> 
</ComboBox> 

...你會看到,設置Foreground確實改變所選項目的文本。我認爲你的問題可能在於你的ControlTemplate。現在,我不能100%肯定,但我本來以爲你ContentPresenter應該是這樣的,而不是:

<ContentPresenter Margin="2" IsHitTestVisible="False" VerticalAlignment="Center" 
    HorizontalAlignment="Stretch" Name="ContentSite" ContentTemplate="{TemplateBinding 
    SelectionBoxItemTemplate}" Content="{TemplateBinding SelectionBoxItem}" /> 
相關問題