wpf 根据数据更改所选 ListBox 项的背景/突出显示颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18461873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:30:38  来源:igfitidea点击:

Change background/highlight-color of selected ListBox item based on data

wpfxamldata-binding

提问by Robert Massa

I want to change the background/hightlight color of a selectedListBox item using data binding. This is what I have and tried, but it's not working. I'm not sure how to get the resources section to have the "current item" context.

我想使用数据绑定更改所选ListBox 项目的背景/突出显示颜色。这是我已经尝试过的,但它不起作用。我不确定如何让资源部分具有“当前项目”上下文。

Color is a property on every item displayed (and has a different value for each item).

颜色是显示的每个项目的属性(每个项目都有不同的值)。

<ListBox x:Name="Categorie" ItemsSource="{Binding Categories}" IsSynchronizedWithCurrentItem="True">
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Style.Resources>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="{Binding /Color}" />
            </Style.Resources>
        </Style>
    </ListBox.ItemContainerStyle>
    <ListBox.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="{Binding /Color}" />
    </ListBox.Resources>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Name}" HorizontalAlignment="Center"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

采纳答案by AsitK

another work around can be to set the background using your color property and setting the HightlightBrushKey to Transparent.

另一种解决方法是使用您的颜色属性设置背景并将 HightlightBrushKey 设置为透明。

回答by Sheridan

You can use a Triggerto achieve that:

您可以使用 aTrigger来实现:

<Style TargetType="ListBoxItem">
    <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Background">
                <Setter.Value>
                    <SolidColorBrush Color="{Binding Color}" />
                </Setter.Value>
            </Setter>
        </Trigger>
    </Style.Triggers>
</Style>

Note that for this binding to work, you musthave a property called Coloron the data item objects that are set as the ListBox.ItemsSource.

请注意,此绑定工作,你必须有一个叫做财产Color上被设置为数据项的对象ListBox.ItemsSource

UPDATE >>>

更新 >>>

Ok, so that was my bad... I forgot that SolidColorBrushis not a FrameworkElementand is Freezable, so they cannot be used in a Binding... you have a few options:

好吧,那是我的错...我忘记了它SolidColorBrush不是 aFrameworkElement和 is Freezable,所以它们不能用于 a Binding......你有几个选择:

Either create your colour(s) as StaticResourceobject(s):

要么将您的颜色创建为StaticResource对象:

<Trigger Property="IsSelected" Value="True">
    <Setter Property="Background" Value="{StaticResource Color}" />
</Trigger>

Or you could bind the Backgroundproperty to the Colorobject using a Converterof some kind:

或者您可以使用某种类型将Background属性绑定到Color对象Converter

<Style TargetType="ListBoxItem" Background="{Binding Color, Converter={StaticResource 
SomeColourConverter}}" />