开发者

How to change background color of ListViewItem dynamically?

开发者 https://www.devze.com 2023-01-07 20:17 出处:网络
I have a BookItemView class for data template of ListViewItem. /// <summary> /// Represent a class to keep every items view.

I have a BookItemView class for data template of ListViewItem.

    /// <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 uniq开发者_运维技巧ue 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();
        }
    }

How can i set an special background color for some of ListViewItems in ListView?


ItemContainerStyle is your friend. For example:

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


Define desired property on what you want to change background and in DataTemplate defined for ListViewItem trigger changing background on specific value of your property.

0

精彩评论

暂无评论...
验证码 换一张
取 消