2016-04-09 141 views
1

我瘋了,我不能改變組合框的顏色。試圖在ComboBox上使用背景屬性,但沒有任何反應。改變組合框的背景色,根本不改變顏色

也試圖使用一個Style塊並設置背景顏色,但那也行不通。

代碼

<ComboBox Padding="7" Height="34" Background="#ffffff"> 
      <ComboBox.Resources> 
       <Style x:Key="{x:Type ComboBox}" TargetType="ComboBox"> 
        <Setter Property="Background" Value="red" /> 
        <Setter Property="BorderThickness" Value="1" /> 
        <Setter Property="BorderBrush" Value="black" /> 
       </Style> 
      </ComboBox.Resources> 
      <ComboBoxItem IsSelected="True">1 - Room</ComboBoxItem> 
      <ComboBoxItem>2 - Rooms</ComboBoxItem> 
      <ComboBoxItem>3 - Rooms</ComboBoxItem> 
      <ComboBoxItem>4 - Rooms</ComboBoxItem> 
      <ComboBoxItem>5+ - Rooms</ComboBoxItem> 
     </ComboBox> 

即使我已經設置背景顏色爲白色,它仍然只是標準的灰色。

在這裏你可以看到它的外觀:

enter image description here

希望有人能告訴我,我做錯了什麼?

+0

看看這個[答案](http://stackoverflow.com/questions/22695145/wpf-change-background-color-of-a-combobox)。看起來你必須實現你自己的ControlTemplate。 –

回答

1

這裏有一些東西在我看來可以幫助你:

  1. 刪除從ComboBox聲明(背景=「#FFFFFF」)背景的定義。
  2. 將組合項目聲明移至組合容納網格,因爲ItemsControl的容器中的項目已被ItemTemplate和ItemTemplateSelector忽略。
  3. 實現數據模板選擇器以支持組合的數據模板(一個用於選定項目,第二個用於選擇項目)。

這裏是XAML代碼

<Grid> 
    <Grid.Resources> 
     <x:Array Type="{x:Type system:String}" x:Key="MyRoomsArray"> 
      <system:String>1 - Room</system:String> 
      <system:String>2 - Rooms</system:String> 
      <system:String>3 - Rooms</system:String> 
      <system:String>4 - Rooms</system:String> 
      <system:String>5+ - Rooms</system:String> 
     </x:Array> 
    </Grid.Resources> 
    <ComboBox Padding="7" Height="34" SelectedIndex="0" ItemsSource="{StaticResource MyRoomsArray}"> 
     <ComboBox.Resources> 
      <DataTemplate x:Key="ItemToSelect"> 
       <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch"> 
        <Border HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 
          Background="Red" 
          BorderBrush="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ComboBox}, Path=BorderBrush, UpdateSourceTrigger=PropertyChanged}" 
          BorderThickness ="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ComboBox}, Path=BorderThickness, UpdateSourceTrigger=PropertyChanged}"> 
         <TextBlock HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Text="{Binding }" /> 
        </Border> 
       </Grid> 
      </DataTemplate> 
      <DataTemplate x:Key="SelectedItem"> 
       <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch"> 
        <Border HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 
          Background="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ComboBox}, Path=Background, UpdateSourceTrigger=PropertyChanged}" 
          BorderBrush="Transparent" 
          BorderThickness ="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ComboBox}, Path=BorderThickness, UpdateSourceTrigger=PropertyChanged}"> 
         <TextBlock HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Text="{Binding }" /> 
        </Border> 
       </Grid> 
      </DataTemplate> 
      <wpfComboBAckground:ComboDataTemplateSelector x:Key="ComboDataTemplateSelector" Selected="{StaticResource SelectedItem}" ItemToSelect="{StaticResource ItemToSelect}"/> 
      <Style TargetType="ComboBox"> 
       <Setter Property="HorizontalContentAlignment" Value="Stretch"/> 
       <Setter Property="VerticalContentAlignment" Value="Stretch"/> 
       <Setter Property="Background" Value="Red" /> 
       <Setter Property="BorderThickness" Value="1" /> 
       <Setter Property="BorderBrush" Value="Black" /> 
       <Setter Property="ItemTemplateSelector" Value="{StaticResource ComboDataTemplateSelector}"/> 
       <Style.Triggers> 
        <Trigger Property="IsMouseOver" Value="True"> 
         <Setter Property="Background" Value="Transparent"></Setter> 
        </Trigger> 
        <Trigger Property="IsMouseOver" Value="False"> 
         <Setter Property="Background" Value="Red"></Setter> 
        </Trigger> 
       </Style.Triggers> 
      </Style> 
     </ComboBox.Resources> 
    </ComboBox> 
</Grid> 

這裏是數據模板選擇

public class ComboDataTemplateSelector : DataTemplateSelector 
{ 
    public override DataTemplate SelectTemplate(object item, DependencyObject container) 
    { 
     var selected = false; 
     // container is the ContentPresenter 
     FrameworkElement fe = container as FrameworkElement; 
     if (fe == null) return ItemToSelect; 
     var cbo = fe.TemplatedParent as ComboBox; 

     if (cbo != null) 
      selected = true; 

     return selected ? Selected : ItemToSelect; 
    } 

    public DataTemplate Selected { get; set; } 

    public DataTemplate ItemToSelect { get; set; } 
} 

如何,它看起來像: here

此致敬禮。