对列表视图中的每一行进行颜色编码
本文关键字:一行 编码 颜色 视图 列表 | 更新日期: 2023-09-27 18:36:19
让我先说我是MVVM
新手,所以请放心,如果问题不清楚,请告诉我,我会尽力澄清。
我有一个成功绑定列表视图的按钮。(它填充列表视图)。下面是按钮 VM 代码:
<Button Content="Fetch Data" Command="{Binding readFilesCommand}" CommandParameter="{Binding Path=Text, ElementName=browseFolderTextBox}" Name="button1" />
正在填充的列表视图如下所示:
<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>
下面是在列表视图中填充的类的代码。
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; }
}
到目前为止,一切正常。现在谈谈我的问题。我希望列表视图的每一行都有一个背景色。假设一行缺少属性时间,那么我希望整行都是红色的。我应该从哪里开始?
可以用容器(使用模板)将每个ListViewItem
括起来,并使用绑定或 DataTrigger 更改此容器的背景颜色。
此处的一个例子:更改列表视图中网格视图行的背景颜色
编辑:根据 MVVM,您应该在类Resource
上定义一个业务属性,类型boolean
(IsValid ?)或枚举(状态?),并使用绑定中的转换器将值转换为SolidColorBrush
(例如)。