首页 文章

选中和未聚焦时更改ListBoxItem背景的颜色

提问于
浏览
1

我试图更改列表框中所选项目的背景颜色 . 之前我通过使用它做到了

<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Yellow" />

哪个有效 . 但是如果我在ListBox上将IsEnabled设置为false,则ListBox的整个背景将变为ControlBrush的指定颜色 . 如果选择了ListBoxItem并且ListBox没有焦点,我只想更改颜色 .

我尝试了一些带触发器的品种,但我无法让它发挥作用 . 即使是包含IsSelected和IsFocused条件的多重触发器也不适用于我 .

有没有人有我的解决方案?

Edit: 用ItemContainerStyle尝试了我在项目中获得NullReferenceException的示例 . 它在新的解决方案中有效 . 那个's the code where it doesn'工作:

<ItemsControl ItemsSource="{Binding Path=Classification.Values}" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                                 HorizontalAlignment="Stretch" VerticalAlignment="Stretch" IsEnabled="{Binding Path=ClassificationEnabled}"
                                  VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" Grid.Row="0" x:Name="measureClassificationControl">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <Grid Margin="2">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="Auto"/>
                                </Grid.RowDefinitions>
                                <TextBlock Text="{Binding Category}"/>
                                <ListBox ItemsSource="{Binding Values.SortedList}" SelectionMode="Extended" Grid.Row="1"  AlternationCount="2" 
                                     SelectionChanged="ListBox_SelectionChanged" ScrollViewer.CanContentScroll="True" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Disabled" Height="120">
                                    <ListBox.ItemTemplate>
                                        <DataTemplate>
                                            <TextBlock TextWrapping="NoWrap" Text="{Binding Key}">
                                            <TextBlock.ToolTip>
                                                <ToolTip>
                                                     <TextBlock TextWrapping="NoWrap" Text="{Binding Value}"/>
                                                </ToolTip>
                                            </TextBlock.ToolTip>
                                        </TextBlock>
                                        </DataTemplate>
                                    </ListBox.ItemTemplate>
                                </ListBox>

                            </Grid>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <UniformGrid Columns="2"/>
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                </ItemsControl>

3 回答

  • 0

    SystemColors.ControlBrushKey 添加到 ItemContainerStyle 中 . 这样它只会影响所选项目 .

    Edit: 这是一个完整的Xaml示例 .

    <StackPanel>
        <ListBox IsEnabled="{Binding ElementName=enabledButton, Path=IsChecked}">
            <ListBox.ItemContainerStyle>
                <Style TargetType="ListBoxItem">
                    <Style.Resources>
                        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}"
                                         Color="Yellow" />
                    </Style.Resources>
                </Style>
            </ListBox.ItemContainerStyle>
            <ListBoxItem>Item 1</ListBoxItem>
            <ListBoxItem>Item 2</ListBoxItem>
            <ListBoxItem>Item 3</ListBoxItem>
            <ListBoxItem>Item 4</ListBoxItem>
            <ListBoxItem>Item 5</ListBoxItem>
        </ListBox>
        <ToggleButton Name="enabledButton" IsChecked="True" Content="IsEnabled"/>
    </StackPanel>
    
  • 1
    • 你不能同时拥有 IsSelected TrueIsEnabled False . 另外 IsFocused TrueIsEnabled False .

    • 您的问题将 IsSelected 的背景颜色更改称为 TrueIsFocused 为False .

    在下面的代码中(只是在你的窗口代码中粘贴 ListBox XAML)...实现了所有这些状态组合...要检查某些东西是否已被聚焦但未被选中,你必须选择一些行然后使用"Control + Up / Down Arrow keys"来松散焦点从选定的行中聚焦其他未选中的行 .

    您还将观察到所选行背景的 Orange 颜色不会出现(虽然 Foreground 颜色 Cyan 也有效) . 对于该背景颜色更改,您必须覆盖ListBoxItem的template .

    <Window x:Class="WpfApplicationThemeTest.Window2"
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:Microsoft.Sample.Controls"
                xmlns:sys="clr-namespace:System;assembly=mscorlib"
                Title="Window1">
            <ListBox>
                <ListBox.Resources>
                    <Style TargetType="{x:Type ListBoxItem}">
                        <Style.Triggers>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <!--Condition Property="IsEnabled" Value="False"/-->
                                    <Condition Property="IsFocused" Value="True"/>
                                    <Condition Property="IsSelected" Value="False"/>
                                </MultiTrigger.Conditions>
                                <Setter Property="Background" Value="Yellow"/>
                            </MultiTrigger>
                            <DataTrigger Binding="{Binding}" Value="My Value6">
                                <Setter Property="IsEnabled" Value="False"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </ListBox.Resources>
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding}"/>
                    </DataTemplate>
                </ListBox.ItemTemplate>
                <ListBox.ItemsSource>
                    <x:Array Type="{x:Type sys:String}">
                        <sys:String>My Value1</sys:String>
                        <sys:String>My Value2</sys:String>
                        <sys:String>My Value3</sys:String>
                        <sys:String>My Value4</sys:String>
                        <sys:String>My Value5</sys:String>
                        <sys:String>My Value6</sys:String>
                        <sys:String>My Value7</sys:String>
                        <sys:String>My Value8</sys:String>
                    </x:Array>
                </ListBox.ItemsSource>
            </ListBox>
        </Window>
    

    我希望这能指导你正确的方向 .

  • 0

    现在解决了这个问题 . 问题似乎是当系统颜色被多次覆盖时XamlParser不喜欢它 . 所以我这样定义:

    <Window x:Class="DataGridTest.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:dg="http://schemas.microsoft.com/wpf/2008/toolkit" xmlns:DataGridTest="clr-namespace:DataGridTest"
            Title="MainWindow" Height="350" Width="525">
        <Window.DataContext>
            <DataGridTest:VM />
        </Window.DataContext>
        <StackPanel>
            <ItemsControl ItemsSource="{Binding Values}">
                <ItemsControl.Resources>
                    <Style TargetType="ListBoxItem">
                        <Style.Resources>
                            <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}"
                                         Color="Yellow" />
                        </Style.Resources>
                    </Style>
                </ItemsControl.Resources>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <ListBox IsEnabled="{Binding ElementName=enabledButton, Path=IsChecked}">
                            <ListBoxItem>Item 1</ListBoxItem>
                            <ListBoxItem>Item 2</ListBoxItem>
                            <ListBoxItem>Item 3</ListBoxItem>
                            <ListBoxItem>Item 4</ListBoxItem>
                            <ListBoxItem>Item 5</ListBoxItem>
                        </ListBox>
    
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
    
            </ItemsControl>
            <ToggleButton Name="enabledButton" IsChecked="True" Content="IsEnabled"/>
        </StackPanel>
    </Window>
    

    谢谢你的帮助!

相关问题