如何动态更改ListViewItem的背景颜色?

时间:2010-07-20 06:49:28

标签: c# wpf listviewitem

我有一个用于ListViewItem数据模板的BookItemView类。

    /// <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();
        }
    }

如何为ListView中的某些ListViewItem设置特殊的背景颜色?

2 个答案:

答案 0 :(得分:1)

ItemContainerStyle是你的朋友。例如:

<ListView>
    <ListView.ItemContainerStyle>
        <Setter Property="Background" Value="{Binding Marked, Converter={StaticResource MarkedConverter}}"/>
    </ListView.ItemContainerStyle>
</ListView>

答案 1 :(得分:1)

为要更改背景的内容定义所需属性,并在为ListViewItem定义的DataTemplate中定义更改属性特定值的背景。