2015-11-01 82 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