ListView 项目颜色冲突

发布于 2024-10-16 13:43:03 字数 4014 浏览 5 评论 0原文

鼠标悬停和键盘聚焦时项目呈绿色。当鼠标悬停在某些项目上时,如何将所选项目的颜色恢复为白色?

就像我必须分离机制一样。

列表的屏幕截图

XAML:

<Window.Resources>
    <SolidColorBrush x:Key="ListBorder" Color="Green"/>
    <SolidColorBrush x:Key="Brush_ListItem_MO" Color="Green"/>
    <SolidColorBrush x:Key="Brush_Tree_Item_Border" Color="Green"/>
    <Style TargetType="ListBoxItem">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Grid x:Name="container">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition SharedSizeGroup="A" Width="Auto" />
                            <ColumnDefinition />
                        </Grid.ColumnDefinitions>
                        <Border x:Name="Border" Grid.ColumnSpan="3" Height="35"
                                HorizontalAlignment="Stretch" />
                        <TextBlock Text="{TemplateBinding Content}"/>
                    </Grid>

                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="BorderBrush" TargetName="Border" Value="{DynamicResource ListBorder}" />
                            <Setter Property="Background" TargetName="Border" Value="{DynamicResource Brush_ListItem_MO}" />
                            <Setter Property="BorderBrush" TargetName="Border" Value="{DynamicResource Brush_Tree_Item_Border}" />
                            <Setter Property="BorderThickness" TargetName="Border" Value="1" />
                        </Trigger>
                        <Trigger Property="IsSelected" Value="true">
                            <Setter Property="BorderBrush" TargetName="Border" Value="{DynamicResource ListBorder}" />
                            <Setter Property="Background" TargetName="Border" Value="{DynamicResource Brush_ListItem_MO}" />
                        </Trigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsSelected" Value="true" />
                                <Condition Property="Selector.IsSelectionActive" Value="false" />
                            </MultiTrigger.Conditions>
                            <Setter Property="BorderBrush" TargetName="Border" Value="Red" />
                            <Setter Property="Background" TargetName="Border" Value="Red" />
                            <Setter Property="BorderBrush" TargetName="Border" Value="{DynamicResource Brush_Tree_Item_Border}" />
                            <Setter Property="BorderThickness" TargetName="Border" Value="1" />
                        </MultiTrigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
                        </Trigger>
                        <!--fgdfgdfgfdgfdgfg-->
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<Grid>
    <ListBox >
        <ListBox.Items>
            <ListBoxItem>1</ListBoxItem>
            <ListBoxItem>2</ListBoxItem>
            <ListBoxItem>3</ListBoxItem>
            <ListBoxItem>4</ListBoxItem>
            <ListBoxItem>5</ListBoxItem>
        </ListBox.Items>
    </ListBox>        
</Grid>

Items are colored in green on mouse-over and on keyboard focused. How can i return the colouring of the selected items to white while the mouse is over some item?

it's like I have to separated mechanisms.

A screenshot of the list

XAML:

<Window.Resources>
    <SolidColorBrush x:Key="ListBorder" Color="Green"/>
    <SolidColorBrush x:Key="Brush_ListItem_MO" Color="Green"/>
    <SolidColorBrush x:Key="Brush_Tree_Item_Border" Color="Green"/>
    <Style TargetType="ListBoxItem">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Grid x:Name="container">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition SharedSizeGroup="A" Width="Auto" />
                            <ColumnDefinition />
                        </Grid.ColumnDefinitions>
                        <Border x:Name="Border" Grid.ColumnSpan="3" Height="35"
                                HorizontalAlignment="Stretch" />
                        <TextBlock Text="{TemplateBinding Content}"/>
                    </Grid>

                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="BorderBrush" TargetName="Border" Value="{DynamicResource ListBorder}" />
                            <Setter Property="Background" TargetName="Border" Value="{DynamicResource Brush_ListItem_MO}" />
                            <Setter Property="BorderBrush" TargetName="Border" Value="{DynamicResource Brush_Tree_Item_Border}" />
                            <Setter Property="BorderThickness" TargetName="Border" Value="1" />
                        </Trigger>
                        <Trigger Property="IsSelected" Value="true">
                            <Setter Property="BorderBrush" TargetName="Border" Value="{DynamicResource ListBorder}" />
                            <Setter Property="Background" TargetName="Border" Value="{DynamicResource Brush_ListItem_MO}" />
                        </Trigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsSelected" Value="true" />
                                <Condition Property="Selector.IsSelectionActive" Value="false" />
                            </MultiTrigger.Conditions>
                            <Setter Property="BorderBrush" TargetName="Border" Value="Red" />
                            <Setter Property="Background" TargetName="Border" Value="Red" />
                            <Setter Property="BorderBrush" TargetName="Border" Value="{DynamicResource Brush_Tree_Item_Border}" />
                            <Setter Property="BorderThickness" TargetName="Border" Value="1" />
                        </MultiTrigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
                        </Trigger>
                        <!--fgdfgdfgfdgfdgfg-->
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<Grid>
    <ListBox >
        <ListBox.Items>
            <ListBoxItem>1</ListBoxItem>
            <ListBoxItem>2</ListBoxItem>
            <ListBoxItem>3</ListBoxItem>
            <ListBoxItem>4</ListBoxItem>
            <ListBoxItem>5</ListBoxItem>
        </ListBox.Items>
    </ListBox>        
</Grid>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

纵山崖 2024-10-23 13:43:03

我使用 MultiTrigger 来分离场景。以下是场景:

  • 鼠标悬停且未选定(绿色)
  • 选定且未鼠标悬停(绿色)
  • 鼠标悬停并选定(浅绿色)

XAML:

<Window.Resources>
    <SolidColorBrush x:Key="ListBorder"
                     Color="Green" />
    <SolidColorBrush x:Key="Brush_ListItem_MO"
                     Color="Green" />
    <SolidColorBrush x:Key="Brush_Tree_Item_Border"
                     Color="Green" />
    <SolidColorBrush x:Key="Brush_ListItem_MO_Selected"
                     Color="LightGreen" />
    <Style TargetType="ListBoxItem">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Grid x:Name="container">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition SharedSizeGroup="A"
                                              Width="Auto" />
                            <ColumnDefinition />
                        </Grid.ColumnDefinitions>
                        <Border x:Name="Border"
                                Grid.ColumnSpan="3"
                                Height="35"
                                HorizontalAlignment="Stretch" />
                        <TextBlock Text="{TemplateBinding Content}" />
                    </Grid>
                    <ControlTemplate.Triggers>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsSelected"
                                           Value="False" />
                                <Condition Property="IsMouseOver"
                                           Value="True" />
                            </MultiTrigger.Conditions>
                            <Setter Property="BorderBrush"
                                    TargetName="Border"
                                    Value="{DynamicResource ListBorder}" />
                            <Setter Property="Background"
                                    TargetName="Border"
                                    Value="{DynamicResource Brush_ListItem_MO}" />
                            <Setter Property="BorderBrush"
                                    TargetName="Border"
                                    Value="{DynamicResource Brush_Tree_Item_Border}" />
                            <Setter Property="BorderThickness"
                                    TargetName="Border"
                                    Value="1" />
                        </MultiTrigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsSelected"
                                           Value="True" />
                                <Condition Property="IsMouseOver"
                                           Value="False" />
                            </MultiTrigger.Conditions>
                            <Setter Property="BorderBrush"
                                    TargetName="Border"
                                    Value="{DynamicResource ListBorder}" />
                            <Setter Property="Background"
                                    TargetName="Border"
                                    Value="{DynamicResource Brush_ListItem_MO}" />
                        </MultiTrigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsSelected"
                                           Value="True" />
                                <Condition Property="IsMouseOver"
                                           Value="True" />
                            </MultiTrigger.Conditions>
                            <Setter Property="BorderBrush"
                                    TargetName="Border"
                                    Value="{DynamicResource ListBorder}" />
                            <Setter Property="Background"
                                    TargetName="Border"
                                    Value="{DynamicResource Brush_ListItem_MO_Selected}" />
                        </MultiTrigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsSelected"
                                           Value="true" />
                                <Condition Property="Selector.IsSelectionActive"
                                           Value="false" />
                            </MultiTrigger.Conditions>
                            <Setter Property="BorderBrush"
                                    TargetName="Border"
                                    Value="Red" />
                            <Setter Property="Background"
                                    TargetName="Border"
                                    Value="Red" />
                            <Setter Property="BorderBrush"
                                    TargetName="Border"
                                    Value="{DynamicResource Brush_Tree_Item_Border}" />
                            <Setter Property="BorderThickness"
                                    TargetName="Border"
                                    Value="1" />
                        </MultiTrigger>
                        <Trigger Property="IsEnabled"
                                 Value="false">
                            <Setter Property="Foreground"
                                    Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
                        </Trigger>
                        <!--fgdfgdfgfdgfdgfg-->
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<Grid>
    <ListBox>
        <ListBox.Items>
            <ListBoxItem>1</ListBoxItem>
            <ListBoxItem>2</ListBoxItem>
            <ListBoxItem>3</ListBoxItem>
            <ListBoxItem>4</ListBoxItem>
            <ListBoxItem>5</ListBoxItem>
        </ListBox.Items>
    </ListBox>
</Grid>

I separated the scenarios using MultiTrigger. Following are the scenarios:

  • Is Mouse Over and Not Selected (Green)
  • Is Selected and Not Mouse OVer (Green)
  • Is Mouse Over and Selected (Light Green)

XAML:

<Window.Resources>
    <SolidColorBrush x:Key="ListBorder"
                     Color="Green" />
    <SolidColorBrush x:Key="Brush_ListItem_MO"
                     Color="Green" />
    <SolidColorBrush x:Key="Brush_Tree_Item_Border"
                     Color="Green" />
    <SolidColorBrush x:Key="Brush_ListItem_MO_Selected"
                     Color="LightGreen" />
    <Style TargetType="ListBoxItem">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Grid x:Name="container">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition SharedSizeGroup="A"
                                              Width="Auto" />
                            <ColumnDefinition />
                        </Grid.ColumnDefinitions>
                        <Border x:Name="Border"
                                Grid.ColumnSpan="3"
                                Height="35"
                                HorizontalAlignment="Stretch" />
                        <TextBlock Text="{TemplateBinding Content}" />
                    </Grid>
                    <ControlTemplate.Triggers>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsSelected"
                                           Value="False" />
                                <Condition Property="IsMouseOver"
                                           Value="True" />
                            </MultiTrigger.Conditions>
                            <Setter Property="BorderBrush"
                                    TargetName="Border"
                                    Value="{DynamicResource ListBorder}" />
                            <Setter Property="Background"
                                    TargetName="Border"
                                    Value="{DynamicResource Brush_ListItem_MO}" />
                            <Setter Property="BorderBrush"
                                    TargetName="Border"
                                    Value="{DynamicResource Brush_Tree_Item_Border}" />
                            <Setter Property="BorderThickness"
                                    TargetName="Border"
                                    Value="1" />
                        </MultiTrigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsSelected"
                                           Value="True" />
                                <Condition Property="IsMouseOver"
                                           Value="False" />
                            </MultiTrigger.Conditions>
                            <Setter Property="BorderBrush"
                                    TargetName="Border"
                                    Value="{DynamicResource ListBorder}" />
                            <Setter Property="Background"
                                    TargetName="Border"
                                    Value="{DynamicResource Brush_ListItem_MO}" />
                        </MultiTrigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsSelected"
                                           Value="True" />
                                <Condition Property="IsMouseOver"
                                           Value="True" />
                            </MultiTrigger.Conditions>
                            <Setter Property="BorderBrush"
                                    TargetName="Border"
                                    Value="{DynamicResource ListBorder}" />
                            <Setter Property="Background"
                                    TargetName="Border"
                                    Value="{DynamicResource Brush_ListItem_MO_Selected}" />
                        </MultiTrigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsSelected"
                                           Value="true" />
                                <Condition Property="Selector.IsSelectionActive"
                                           Value="false" />
                            </MultiTrigger.Conditions>
                            <Setter Property="BorderBrush"
                                    TargetName="Border"
                                    Value="Red" />
                            <Setter Property="Background"
                                    TargetName="Border"
                                    Value="Red" />
                            <Setter Property="BorderBrush"
                                    TargetName="Border"
                                    Value="{DynamicResource Brush_Tree_Item_Border}" />
                            <Setter Property="BorderThickness"
                                    TargetName="Border"
                                    Value="1" />
                        </MultiTrigger>
                        <Trigger Property="IsEnabled"
                                 Value="false">
                            <Setter Property="Foreground"
                                    Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
                        </Trigger>
                        <!--fgdfgdfgfdgfdgfg-->
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<Grid>
    <ListBox>
        <ListBox.Items>
            <ListBoxItem>1</ListBoxItem>
            <ListBoxItem>2</ListBoxItem>
            <ListBoxItem>3</ListBoxItem>
            <ListBoxItem>4</ListBoxItem>
            <ListBoxItem>5</ListBoxItem>
        </ListBox.Items>
    </ListBox>
</Grid>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文