2015-11-01 90 views
1

我想改變ComboBoxItem的前景色,但它不適用,我做錯了什麼?另外我試圖改變ComboBoxItem徘徊的前景顏色,這不起作用。更改ComboBoxItem的前景色

這是我的XAML:

所有我想知道,如果你看到 Label內容的
<ComboBox x:Name="tab5_2_num" ItemsSource="{Binding}" FontSize="13" FontFamily="/WPF;component/Font/#Agency FB" Margin="722,46,406,281" BorderThickness="1,1,1,4" Height="30"> 
    <ComboBox.ItemTemplate> 
     <DataTemplate> 
      <Label Content="{Binding}" /> 
     </DataTemplate> 
    </ComboBox.ItemTemplate> 
    <ComboBox.ItemContainerStyle> 
     <Style TargetType="{x:Type ComboBoxItem}"> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type ComboBoxItem}"> 
         <Label x:Name="lblCombo" Foreground="Black" FontFamily="/WPF;component/Font/#Agency FB" FontSize="13" Height="20" /> 
         <ControlTemplate.Triggers> 
          <Trigger Property="IsMouseOver" Value="True"> 
           <Setter TargetName="lblCombo" Property="Background" Value="#FFF01F1F"/> 
           <Setter TargetName="lblCombo" Property="Foreground" Value="White" /> 
          </Trigger> 
         </ControlTemplate.Triggers> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </ComboBox.ItemContainerStyle> 
</ComboBox> 
+0

請問您是如何知道這不起作用的?任何特定的錯誤消息或表現? – tgpdyk

+0

其實我是在做反向,看@JayZuo回答它的工作:) – Valkyry

回答

1

既然你已經設置你的ComboBoxItem的模板Label,在LabelDataTemplate將無法​​正常工作。所以請嘗試以下代碼:

<ComboBox x:Name="tab5_2_num" Height="30" BorderThickness="1,1,1,4" FontFamily="/WPF;component/Font/#Agency FB" FontSize="13" ItemsSource="{Binding}"> 
    <ComboBox.ItemContainerStyle> 
     <Style TargetType="{x:Type ComboBoxItem}"> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type ComboBoxItem}"> 
         <Label x:Name="lblCombo" Content="{Binding}" FontFamily="/WPF;component/Font/#Agency FB" FontSize="13" Foreground="Black" /> 
         <ControlTemplate.Triggers> 
          <Trigger Property="IsMouseOver" Value="True"> 
           <Setter TargetName="lblCombo" Property="Background" Value="#FFF01F1F" /> 
           <Setter TargetName="lblCombo" Property="Foreground" Value="White" /> 
          </Trigger> 
         </ControlTemplate.Triggers> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </ComboBox.ItemContainerStyle> 
</ComboBox> 

它應該有效。

+0

工作就像一個魅力,只需要給Content =「{Binding}」給標籤打擊,謝謝 – Valkyry

1

第一。您可能需要執行以下操作:

<Label Content={Binding} ... /> 
+0

我正在做反向大聲笑。謝謝 – Valkyry