2013-11-22 79 views
0

我目前正在嘗試爲組合框中選定的項目顯示正確的樣式。我這樣做的原因是,我對ComboBox如何顯示所選項目沒有太多控制,例如 - 在深色背景下,該項目仍然顯示爲黑色。根據內容的樣式

我想出了以下解決方案:

<DataTemplate x:Key="MyItem" DataType="ComboBoxItem"> 
    <TextBlock Text="{Binding}" Foreground="White"/> 
</DataTemplate> 

<!-- (...) --> 

<Style TargetType="{x:Type ComboBox}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate> 

      <!-- ... --> 
      <!-- Displaying currently selected item --> 

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

      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

現在選擇了簡單ComboBoxItem時,它正確地顯示在組合框。另一方面,如果我 - 例如 - 顯示帶有一些內容的按鈕,作爲迴應,我得到文本System.Windows.Shapes.Rectangle,這與我想要顯示的內容遠遠不同。

我想爲ComboBox中顯示的不同數據類型使用不同的模板 - 我將能夠自定義它們的外觀。我怎樣才能做到這一點?


編輯:

要非常清楚,我說的是在這種情況下選擇(=選擇)ComboBox項:

Selected item in combo box

(不是選擇組合框的項目在組合框的列表

回答

1
+0

能否請您提供一個小例子,請問有什麼可以嵌入該ContentTemplateSelector在我的DataTemplate? – Spook

+0

你看過鏈接嗎? –

+0

我做過,但'ComboBox'似乎沒有'ContentTemplateSelector'屬性。 – Spook

0

對,這個問題有幾個不同的方面。

<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" /> 
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" /> 
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="White" /> 
<SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="Black" /> 

嘗試使用這些設置,爲他們兩個定義文本和兩個設置顏色的背景的顏色:首先,我們可以通過添加這個到Resources部分擺脫默認選擇顏色選定的項目。事實上,您可以在這裏使用任何顏色來提供一種快速方法來更改選定的項目顏色......而不必去解決所有的麻煩。但對於你的例子,讓我們把它們作爲Transparent

好了,下次你想有一個不同的外觀出現在ComboBox每個不同的數據類型......這也可以輕鬆實現。所有你需要做的是要申報每個類型DataTemplate不設置x:Key屬性,使他們將被隱式應用。這是一個基本的例子:

<Window.Resources> 
    <DataTemplate DataType="{x:Type Type1}"> 
     <TextBlock Text="{Binding}" Foreground="Red" /> 
    </DataTemplate> 
    <DataTemplate DataType="{x:Type Type2}"> 
     <TextBlock Text="{Binding}" Foreground="Green" /> 
    </DataTemplate> 
    <DataTemplate DataType="{x:Type Type3}"> 
     <TextBlock Text="{Binding}" Foreground="Blue" /> 
    </DataTemplate> 
</Window.Resources> 

如果用這三種這些DataTemplate S的物品填充的ComboBox,那麼項目將在Red有色,綠色and Blue`。

+0

你確定,你說的是同樣的「選定的項目」我嗎?我修改了我的問題一點,以顯示,我在說什麼 – Spook

+0

你是對,我確實認爲你在討論* actual *選擇的項目,但是,在顯示數據類型實例的任何地方,單獨的DataTemplate應該仍然可以工作。 – Sheridan

0

我會留下ContentTemplateSelector所有,誰尋求同樣的問題,解決的方案。

  1. 添加下面的類到您的庫/應用:

    public class ComboBoxSelectionTemplateSelector : DataTemplateSelector 
    { 
    
        public DataTemplate TextTemplate 
        { 
         get; 
         set; 
        } 
    
        public DataTemplate ContentTemplate 
        { 
         get; 
         set; 
        } 
    
        public override System.Windows.DataTemplate SelectTemplate(object item, DependencyObject container) 
        { 
         if (item is string) 
          return TextTemplate; 
         else 
          return ContentTemplate; 
        } 
    } 
    
  2. 定義兩個不同的DataTemplates:

    <DataTemplate x:Key="ComboBoxTextItem"> 
        <TextBlock Text="{Binding}" Foreground="{DynamicResource {x:Static vs:VsBrushes.WindowTextKey}}" /> 
    </DataTemplate> 
    
    <DataTemplate x:Key="ComboBoxOtherItem"> 
        <ContentPresenter Content="{Binding}" /> 
    </DataTemplate> 
    
  3. 實例化要在使用它裏面的風格選擇:

    <Style.Resources> 
        <local:ComboBoxSelectionTemplateSelector ContentTemplate="{StaticResource ComboBoxOtherItem}" 
                  TextTemplate="{StaticResource ComboBoxTextItem}" x:Key="ContentTemplateSelector" /> 
    </Style.Resources> 
    
  4. ContentPresenter使用它:

    <!-- ... --> 
    <ControlTemplate TargetType="ComboBox"> 
        <Grid> 
         <ContentPresenter ContentTemplateSelector="{StaticResource ContentTemplateSelector}" 
              Content="{TemplateBinding ComboBox.SelectionBoxItem}" /> 
         <!-- Other items --> 
         <Popup ... /> <!-- For displaying ComboBox's list --> 
        </Grid>