简体   繁体   English

列表框项目WPF,不同项目的背景颜色不同

[英]Listbox item WPF, different background color for different items

I have a WPF ListBox containing a binded list of items from a specific class that I have. 我有一个WPF ListBox,其中包含我所拥有的特定类的项目的绑定列表。 Something like this: 像这样:

    ObservableCollection<MyTable> tables = new ObservableCollection<MyTable>();
...
    listTables.ItemsSource = tables;

And the XAML: 和XAML:

<ListBox HorizontalAlignment="Left" Margin="8,10,0,0" Name="listTables" Width="153" ItemsSource="{Binding tables}" SelectionChanged="listTables_SelectionChanged" Height="501" VerticalAlignment="Top">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid Margin="1">
                    <TextBlock Grid.Column="1" Text="{Binding tableName}" />
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

All works fine. 一切正常。 What I want to do now is have a different background for each item in the ListBox depending on a certain property of the class. 我现在想做的是根据类的某个属性为ListBox中的每个项目设置不同的背景。 For example, let's say that the MyTable class has a property called isOccupied. 例如,假设MyTable类具有一个名为isOccupied的属性。 If this flag is set for a certain item, I want it to have a red background in the ListBox, if it's not, then I want to have it with a green background. 如果为某个项目设置了此标志,则我希望它在ListBox中具有红色背景,如果不是,则希望具有绿色背景。 If the property changes, then the background should change accordingly. 如果属性更改,则背景也应相应更改。

Any tips on how to achieve this? 有关如何实现此目标的任何提示? I'm looking up some information regarding ItemContainerStyle at the moment but I'm relatively new to this so I'm not sure if I'm following the right path. 我目前正在查找有关ItemContainerStyle的一些信息,但是我对此还比较陌生,所以我不确定我是否遵循正确的道路。

You can achieve that using DataTrigger s 您可以使用DataTrigger实现

<ListBox.ItemTemplate>
    <DataTemplate>

        <!-- Step #1: give an x:Name to this Grid -->
        <Grid Margin="1" x:Name="BackgroundGrid">
            <TextBlock Grid.Column="1" Text="{Binding tableName}" />
        </Grid>

        <!-- Step #2: create a DataTrigger that sets the Background of the Grid, depending on the value of IsOccupied property in the Model -->             
        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding IsOccupied}" Value="True">
                <Setter TargetName="BackgroundGrid" Property="Background" Value="Red"/>
            </DataTrigger>

            <DataTrigger Binding="{Binding IsOccupied}" Value="False">
                <Setter TargetName="BackgroundGrid" Property="Background" Value="Green"/>
            </DataTrigger>
        </DataTemplate.Triggers>
    </DataTemplate>
</ListBox.ItemTemplate>

Keep in mind that if you expect these values to change at runtime, your data item must properly implement and raise Property Change Notifications : 请记住,如果您希望这些值在运行时发生更改,则您的数据项必须正确实现并引发“ 属性更改通知”

public class MyTable: INotifyPropertyChanged //Side comment: revise your naming conventions, this is not a table.
{
   private bool _isOccupied;
   public bool IsOccupied
   {
       get { return _isOccupied; }
       set
       {
           _isOccupied = value;
           NotifyPropertyChange("IsOccupied");
       }
    }

    //.. Other members here..
}
<Style TargetType="ListBox" x:Key="myListBoxStyle">
    <Style.Triggers>
        <DataTrigger Binding="{Binding SelectedItem.IsOccupied}" Value="True">
            <Setter Property="Background" Value="Red" />
        </DataTrigger>
    </Style.Triggers>
</Style>

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

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