C# 如何动态更改ListViewItem的背景色?
我有一个BookItemView类用于ListViewItem的数据模板C# 如何动态更改ListViewItem的背景色?,c#,wpf,listviewitem,C#,Wpf,Listviewitem,我有一个BookItemView类用于ListViewItem的数据模板 /// <summary> /// Represent a class to keep every items view. /// </summary> public class BookItemView : ICloneable { /// <summary> /// Create a new instance o
/// <summary>
/// Represent a class to keep every items view.
/// </summary>
public class BookItemView : ICloneable
{
/// <summary>
/// Create a new instance of BookItemView.
/// </summary>
public BookItemView()
{
this.ID = Guid.NewGuid();
}
/// <summary>
/// Gets current instance unique id.
/// </summary>
public Guid ID
{
get;
private set;
}
/// <summary>
/// Gets or sets items title.
/// </summary>
public string Title { get; set; }
/// <summary>
/// Gets or sets items subtitle.
/// </summary>
public string Subtitle { get; set; }
/// <summary>
/// Gets or sets items description.
/// </summary>
public string Description { get; set; }
/// <summary>
/// Gets or sets items icon.
/// </summary>
public ImageSource Icon { get; set; }
/// <summary>
/// Gets or sets a value which indicate is item marked or not.
/// </summary>
public bool Marked { get; set; }
/// <summary>
/// Gets or sets a list of BookItemView.
/// </summary>
public ObservableCollection<BookItemView> BookItems { get; set; }
/// <summary>
/// Create a shallow copy of current object.
/// </summary>
/// <returns>A shallow copy of current object.</returns>
public object Clone()
{
return base.MemberwiseClone();
}
}
//
///表示一个类以保留每个项视图。
///
公共类BookItemView:iClonable
{
///
///创建BookItemView的新实例。
///
公共BookItemView()
{
this.ID=Guid.NewGuid();
}
///
///获取当前实例的唯一id。
///
公共Guid ID
{
得到;
私人设置;
}
///
///获取或设置项目标题。
///
公共字符串标题{get;set;}
///
///获取或设置子标题项。
///
公共字符串字幕{get;set;}
///
///获取或设置项目描述。
///
公共字符串说明{get;set;}
///
///获取或设置项目图标。
///
公共图像源图标{get;set;}
///
///获取或设置一个值,该值指示项是否已标记。
///
标记为{get;set;}的公共布尔
///
///获取或设置BookItemView的列表。
///
公共ObservableCollection BookItems{get;set;}
///
///创建当前对象的浅层副本。
///
///当前对象的浅拷贝。
公共对象克隆()
{
返回base.MemberwiseClone();
}
}
如何为ListView中的某些ListViewItems设置特殊背景色?
ItemContainerStyle
是您的朋友。例如:
<ListView>
<ListView.ItemContainerStyle>
<Setter Property="Background" Value="{Binding Marked, Converter={StaticResource MarkedConverter}}"/>
</ListView.ItemContainerStyle>
</ListView>
在要更改的背景上定义所需的属性,并在为ListViewItem定义的DataTemplate中触发在属性的特定值上更改背景