简体   繁体   English

在WPF中绑定ListBox项背景和选择颜色

[英]Bind ListBox item background & selection colors in WPF

I have a list where the background color of each item should depend on two things: the selection state and a boolean property in the data context. 我有一个列表,其中每个项目的背景色应取决于两件事:选择状态和数据上下文中的布尔属性。 I've figured out how to bind the background color, but as soon as I select the list item my custom background disappears. 我已经弄清楚如何绑定背景颜色,但是一旦选择列表项,我的自定义背景就会消失。

The end result would ideally be (depending on the selection state) a shade of green if the data context boolean is true or a shade of red if it's false. 如果数据上下文布尔值为true,则最终结果理想情况下将是(取决于选择状态)绿色阴影,或者如果为false,则红色阴影。

MainWindow.xaml: MainWindow.xaml:

<ListBox x:Name="CrewList" ItemsSource="{Binding CrewList}" SelectedItem="{Binding SelectedCrew}" Style="{StaticResource EventOverviewListBox}" Grid.Column="1" Grid.Row="0">
            <ListBox.ItemContainerStyle>
                <Style TargetType="{x:Type ListBoxItem}">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Data.IsValid}" Value="true">
                            <Setter Property="Background" Value="{StaticResource StatusValid}"/>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding Data.IsValid}" Value="false">
                            <Setter Property="Background" Value="{StaticResource StatusInvalid}"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </ListBox.ItemContainerStyle>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal" Margin="10,10,10,10" KeyboardNavigation.IsTabStop="False">
                        <TextBlock Text="{Binding Lane}" Margin="0,0,20,0" FontSize="20" />
                        <TextBlock Text="{Binding ClubName}" Foreground="Black" VerticalAlignment="Center"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

App.xaml: App.xaml:

    <SolidColorBrush x:Key="StatusValid">LawnGreen</SolidColorBrush>
    <SolidColorBrush x:Key="StatusInvalid">Red</SolidColorBrush>
    <Style x:Key="EventOverviewListBox" TargetType="ListBox">
        <Setter Property="KeyboardNavigation.IsTabStop" Value="False"/>
        <Setter Property="HorizontalAlignment" Value="Stretch"/>
        <Setter Property="VerticalAlignment" Value="Stretch"/>
        <Setter Property="Background" Value="Teal"/>
    </Style>
    <Style x:Key="EventOverviewListBoxItem" TargetType="ListBoxItem">
        <Setter Property="KeyboardNavigation.IsTabStop" Value="False"/>
    </Style>

I've searched and searched but haven't found anything in the last couple of hours, so I'm hoping someone else knows of a way. 我已经搜索了,但是在最近几个小时内没有找到任何东西,所以我希望其他人知道一种方法。

Thanks! 谢谢!

I'm assuming that by "selection state" you mean if the item is selected or not. 我假设“选择状态”表示是否选择了该项目。

The ListBoxItem has a ControlTemplate with triggers that takes precedence over the triggers. ListBoxItem具有一个带有触发器的ControlTemplate,该模板优先于触发器。 You need to create your own ControlTemplate for the ListBoxItem style. 您需要为ListBoxItem样式创建自己的ControlTemplate。 Also, use a MultiDataTrigger . 另外,请使用MultiDataTrigger

Like this: 像这样:

<Style TargetType="{x:Type ListBoxItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ListBoxItem">
                <Border Name="BorderWrap">
                    <ContentPresenter />
                </Border>
                <ControlTemplate.Triggers>
                    <MultiDataTrigger>
                        <MultiDataTrigger.Conditions>
                            <Condition Binding="{Binding RelativeSource={RelativeSource Self},Path=IsSelected}" Value="True" />
                            <Condition Binding="{Binding Data.IsValid}" Value="True" />
                        </MultiDataTrigger.Conditions>
                        <MultiDataTrigger.Setters>
                            <Setter TargetName="BorderWrap" Property="Background" Value="{StaticResource StatusValid}"/>
                        </MultiDataTrigger.Setters>
                    </MultiDataTrigger>
                    <MultiDataTrigger>
                        <MultiDataTrigger.Conditions>
                            <Condition Binding="{Binding RelativeSource={RelativeSource Self},Path=IsSelected}" Value="True" />
                            <Condition Binding="{Binding Data.IsValid}" Value="False" />
                        </MultiDataTrigger.Conditions>
                        <MultiDataTrigger.Setters>
                            <Setter TargetName="BorderWrap" Property="Background" Value="{StaticResource StatusInvalid}"/>
                        <MultiDataTrigger.Setters>
                    </MultiDataTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM