簡體   English   中英

更改 ComboBoxItem 的前景色

[英]Changing Foreground color of ComboBoxItem

我正在嘗試更改ComboBoxItem的前景色,但它不適用,我做錯了什么? 此外,我正在嘗試更改ComboBoxItem上懸停的前景色,但效果不佳。

這是我的xaml:

<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>

首先我想知道你是否看到了Label內容。 您可能需要以下內容:

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

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

<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>

它應該工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM