对ListView中的每一行进行颜色编码

时间:2013-10-22 10:49:49

标签: c# wpf visual-studio-2010 mvvm colors

首先让我说我是MVVM的新手,所以如果问题不清楚,请告诉我,我会尽力澄清。

我有一个成功绑定ListView的按钮。 (它填充listView)。 下面是按钮VM代码:

<Button Content="Fetch Data" Command="{Binding readFilesCommand}" CommandParameter="{Binding Path=Text, ElementName=browseFolderTextBox}" Name="button1" />

正在填充的listView如下所示:

<ListView SelectionMode="Extended" Name="responseListView" ItemsSource="{Binding}" GridViewColumnHeader.Click="responseListViewClick" >
    <ListView.Resources>
        <local:IndexConverter x:Key="IndexConverter" />
        <DataTemplate x:Key="OrdinalColumnDataTemplate">
            <TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListViewItem},
                Converter={StaticResource ResourceKey=IndexConverter}}" HorizontalAlignment="Right" />
        </DataTemplate>
    </ListView.Resources>
    <ListView.View>
        <GridView x:Name="gridView2" AllowsColumnReorder="True">
            <GridViewColumn Width="28" Header="#" CellTemplate="{StaticResource ResourceKey=OrdinalColumnDataTemplate}" />
            <GridViewColumn Width="80" DisplayMemberBinding="{Binding Name}" Header="Name" />
            <GridViewColumn Width="150" DisplayMemberBinding="{Binding EMail}" Header="EMail" />
            <GridViewColumn Width="75" DisplayMemberBinding="{Binding Date}" Header="Date" />
            <GridViewColumn Width="75" DisplayMemberBinding="{Binding Time}" Header="Time" />
        </GridView>
    </ListView.View>
</ListView>

以下是listView中填充的类的代码。

public class ResourceList : ObservableCollection<Resource>
{
    public ResourceList() : base()
    {
    }
}

public class Resource : INotifyPropertyChanged
{
    public Resource()
    {
        Name = "";
        EMail = "";
        Date = "";
        Time = "";
        SWList = new ObservableCollection<string>();
    }

    private string name;
    private string eMail;
    private string time;
    private string date;

    public string Name
    {
        get { return name;}
        set 
        {
            if(name != value) 
            {
                name = value;
                OnPropertyChanged("Name");
            }
        }
    }

    public string EMail
    {
        get { return eMail; }
        set 
        {
            if (eMail != value) 
            {
                eMail = value;
                OnPropertyChanged("EMail");
            }
        }
    }

    public string Date
    {
        get { return date;}
        set 
        {
            if (date != value) 
            {
                date = value;
                OnPropertyChanged("Date");
            }
        }
    }

    public string Time
    {
        get { return time; }
        set 
        {
            if (time != value) 
            {
                time = value;
                OnPropertyChanged("Time");
            }
        }
    }

    // This interface causes the View to be notified of changes to the instances of Resource.
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if(handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public ObservableCollection<string> SWList { get; set; }
}

// ObservableCollection notifies the View of changes to the collection (add, delete, move items)
public class Licenses : ObservableCollection<Licenses>
{
    public Licenses()
    {
    }
    public string Name { get; set; }
    public string License { get; set; }
}

到目前为止一切正常。现在回答我的问题。我希望ListView的每一行都有背景颜色。假设一行缺少属性时间,那么我希望整行是红色的。我应该从哪里开始?

1 个答案:

答案 0 :(得分:0)

您可以用容器包围每个ListViewItem(使用模板执行此操作)并使用绑定或DataTrigger更改此容器的背景颜色。

这里有一个例子:Change background color of GridView row in a ListView

编辑:根据MVVM,您应该在类Resource上定义一个类型为boolean(IsValid?)或枚举(状态?)的商家属性,并在Binding中使用转换器将值转换为SolidColorBrush(例如)。