Изменить передний план ComboboxItem при наведении курсора - PullRequest
0 голосов
/ 09 июля 2020

Я сейчас пытаюсь изменить Foreground / FontColor моих элементов Combobox, когда они наведены мышью. Я попытался установить значение, отличное от MultiTriggerCondition, чтобы активировать его, но оно все еще не работает.

<SolidColorBrush x:Key="ComboBoxItem2.ItemsviewHover.Foreground" Color="White"/>
<SolidColorBrush x:Key="ComboBoxItem2.ItemsviewSelected.Foreground" Color="White"/>
<Style x:Key="ComboBoxItemStyle2" TargetType="{x:Type ComboBoxItem}">
    <Setter Property="SnapsToDevicePixels" Value="True"/>
    <Setter Property="Padding" Value="4,1"/>
    <Setter Property="Foreground" Value="Gainsboro"/>
    <Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ComboBoxItem}">
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="Opacity" Value="0.7" />
                    </Trigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsSelected" Value="False"/>
                            <Condition Property="IsMouseOver" Value="True"/>
                            <Condition Property="IsKeyboardFocused" Value="False"/>
                        </MultiTrigger.Conditions>
                        <Setter Property="TextElement.Foreground" Value="{StaticResource ComboBoxItem2.ItemsviewHover.Foreground}"/>
                    </MultiTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Ответы [ 2 ]

1 голос
/ 09 июля 2020

У вас нет Contentpresenter в вашем шаблоне, поэтому вы не увидите никаких элементов, когда опустите его.

И вы не можете увидеть белый текст на белом фоне по умолчанию.

Я сделал это так же, как стиль в образце комбинации и фиксированный красный цвет:

    <Style TargetType="{x:Type ComboBoxItem}">
        <Setter Property="SnapsToDevicePixels" Value="True"/>
        <Setter Property="Padding" Value="4,1"/>
        <Setter Property="Foreground" Value="Gainsboro"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ComboBoxItem}">
                    <ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter Property="Opacity" Value="0.7" />
                        </Trigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsSelected" Value="False"/>
                                <Condition Property="IsMouseOver" Value="True"/>
                                <Condition Property="IsKeyboardFocused" Value="True"/>
                            </MultiTrigger.Conditions>
                            <Setter Property="TextElement.Foreground" Value="Red"/>

                        </MultiTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
0 голосов
/ 09 июля 2020

Вы можете достичь sh своей цели, используя ItemContainerStyle вместо ControlTemplate.

Свойство Template предоставляет весь XAML, необходимый для отображения элемента управления. В приведенном выше примере вы, по сути, стерли весь этот XAML и заменили его триггером.

An ItemContainerStyle, однако, применяет стиль к каждому ComboBoxItem. Затем вы можете использовать триггеры стиля для индивидуального выделения. Вот пример:

<ComboBox
    ...>
    <ComboBox.ItemContainerStyle>
        <Style BasedOn="{StaticResource {x:Type ComboBoxItem}}" TargetType="{x:Type ComboBoxItem}">
            <Style.Triggers>
                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsSelected}" Value="False" />
                        <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsMouseOver}" Value="True" />
                    </MultiDataTrigger.Conditions>
                    <Setter Property="TextElement.Foreground" Value="Red" />
                </MultiDataTrigger>
            </Style.Triggers>
        </Style>
    </ComboBox.ItemContainerStyle>
</ComboBox>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...