【问题标题】:Listbox item WPF, different background color for different items列表框项目WPF,不同项目的不同背景颜色
【发布时间】:2013-11-20 15:14:26
【问题描述】:

我有一个 WPF ListBox,其中包含来自我拥有的特定类的项目的绑定列表。像这样的:

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

还有 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>

一切正常。我现在要做的是根据类的某个属性为 ListBox 中的每个项目设置不同的背景。例如,假设 MyTable 类有一个名为 isOccupied 的属性。如果为某个项目设置了此标志,我希望它在 ListBox 中具有红色背景,如果不是,那么我希望它具有绿色背景。如果属性发生变化,那么背景也应该随之变化。

关于如何实现这一点的任何提示?我目前正在查找有关 ItemContainerStyle 的一些信息,但我对此比较陌生,所以我不确定我是否走在正确的道路上。

【问题讨论】:

    标签: c# wpf xaml listbox


    【解决方案1】:

    您可以使用DataTriggers 实现这一目标

    <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>
    

    请记住,如果您希望这些值在运行时发生变化,您的数据项必须正确实现并引发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..
    }
    

    【讨论】:

    • 那么你还不如拥有 Eran 的旧 +1。 ;)
    • 这很有帮助,就像一个魅力。非常感谢!
    【解决方案2】:
    <Style TargetType="ListBox" x:Key="myListBoxStyle">
        <Style.Triggers>
            <DataTrigger Binding="{Binding SelectedItem.IsOccupied}" Value="True">
                <Setter Property="Background" Value="Red" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
    

    【讨论】:

    • +1 现在有一个正确的答案...稍微解释一下(对于需要的用户),但正确的答案都是一样的。
    • @Sheridan 你确定这会达到 OP 的要求吗?检查我的答案
    • +0 你是完全正确的@HighCore ...谢谢,当我在这个网站上时,我需要多加注意。这不仅根本不起作用,而且即使它确实按计划工作,它也只会对选定的项目起作用。
    • @Sheridan 根据所选项目,它将适用于整个列表框
    • @eranotzap,我在 Visual Studio 中尝试过......它没有用。即使确实如此,这也不是作者提出的问题。希望我的 cmets 没有让你不高兴或惹恼你。也许我可以更有礼貌,但我只是说我看到的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-14
    • 2021-05-29
    • 2012-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多