为什么我的ListView的ItemContainerStyle没有正确更新?

时间:2013-07-18 16:12:39

标签: c# wpf listview styles styling

我有一个非常简单的问题。我的代码如下(创建一个名为WPFTesting的简单WPF项目并删除此代码以测试它):

XAML:

<Window x:Class="WPFTesting.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid Name="MainGrid"/>
</Window>

CS:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;

namespace WPFTesting
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        ObservableCollection<Message> messages = new ObservableCollection<Message>();

        public MainWindow()
        {
            InitializeComponent();
            this.KeyDown += MainWindow_KeyDown;
            for (int i = 0; i < 10; i++)
            {
                messages.Add(new Message(DateTime.Now, "This is a test.", false));
            }
            ListView listView = new ListView();
            CreateListViewItemStyle(listView);
            GridView gridView = new GridView();
            listView.View = gridView;
            GridViewColumn timeStampColumn = new GridViewColumn();
            timeStampColumn.DisplayMemberBinding = new Binding("Date");
            GridViewColumnHeader timeStampHeader = new GridViewColumnHeader();
            timeStampHeader.Content = "Time";
            timeStampColumn.Header = timeStampHeader;
            gridView.Columns.Add(timeStampColumn);
            GridViewColumn messageColumn = new GridViewColumn();
            messageColumn.DisplayMemberBinding = new Binding("Text");
            GridViewColumnHeader messageHeader = new GridViewColumnHeader();
            messageHeader.Content = "Message";
            messageColumn.Header = messageHeader;
            gridView.Columns.Add(messageColumn);
            Binding binding = new Binding();
            binding.Source = messages;
            listView.SetBinding(ItemsControl.ItemsSourceProperty, binding);
            MainGrid.Children.Add(listView);
        }

        int clickCount = 0;

        void MainWindow_KeyDown(object sender, KeyEventArgs e)
        {
            if (clickCount % 2 == 0)
            {
                messages[0].IsBookmarked = true;
            }
            else
            {
                messages.Add(new Message(DateTime.Now, "This is a test.", true));
            }
            clickCount++;
        }

        private static void CreateListViewItemStyle(ListView listView)
        {
            string xaml = @"
                            <Style  xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" TargetType=""ListViewItem"">
                                <Setter Property=""BorderThickness"" Value=""0""/>
                                <Setter Property=""Margin"" Value=""0""/>
                                <Setter Property=""Height"" Value=""20""/>
                                <Setter Property=""Template"">
                                    <Setter.Value>
                                    <ControlTemplate TargetType=""{x:Type ListViewItem}"">
                                        <Border Background=""{TemplateBinding Background}""
                                                CornerRadius=""0"">
                                            <GridViewRowPresenter/>
                                        </Border>
                                    </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                                <Style.Triggers>
                                    <DataTrigger Binding=""{Binding IsBookmarked}"" Value=""True"">
                                        <Setter Property=""Background"" Value=""Pink""/>
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>";
            StringReader stringReader = new StringReader(xaml);
            XmlReader xmlReader = XmlReader.Create(stringReader);
            listView.ItemContainerStyle = XamlReader.Load(xmlReader) as Style;
        }

        public class Message
        {

            public Message(DateTime aDate, String aText, bool anIsBookmarked)
            {
                Date = aDate;
                Text = aText;
                IsBookmarked = anIsBookmarked;
            }

            public DateTime Date { get; set; }
            public String Text { get; set; }
            public bool IsBookmarked { get; set; }
        }
    }
}

问题是在MainWindow_KeyDown上,我将设置消息[0] .IsBookmarked = true;在任何偶数个按键上,但这不会更新它的样式,因为它现在应该在列表视图中以粉红色背景显示。但是,使用messages.Add添加新消息(new Message(DateTime.Now,“This is a test。”,true));似乎没有任何问题,因为它已经设置了IsBookmarked标志创建。我的问题是,有没有办法按照我喜欢的方式更新这种风格?

1 个答案:

答案 0 :(得分:2)

消息类需要为要通知的UI实现iNotifyPropertyChanged

INotifyPropertyChanged Interface