我有一个非常简单的问题。我的代码如下(创建一个名为 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 上,我会设置 messages[0].IsBookmarked = true; 在任何偶数次按键上,但这不会更新其样式,因为它现在应该在列表视图中显示为粉红色背景。但是,使用 messages.Add(new Message(DateTime.Now, "This is a test.", true)); 添加新消息 似乎可以正常工作,因为它是使用已设置的 IsBookmarked 标志创建的。我的问题是,有没有办法按照我想要的方式更新这种风格?