我正在尝试生成一个与Visual Studio中的错误列表非常相似的错误列表。
我有一个视图和视图模型。我更新单例视图模型并通知属性发生的更改是实际消息(错误和警告)。但是,网格保持不变 - 出了什么问题?!
我已经介入,集合_messages已正确更新,初始消息显示正确,但在视图模型更新后仍保留在视图中。换句话说,应该根据正确的视图模型更新视图。
我知道RadPane中的错误导致Panes放松了与视图模型的连接,并实现了标准的解决方法。
视图模型实现了INotifyPropertyChanged。
(xaml中的NotifySourceUpdated添加以试图补救,但无济于事)
(由于Stackoverflow限制,Brackets在View中被替换)
感谢您的任何意见和见解......
丹麦安德斯
查看:
[Controls:RadPane x:Class="Rap1D.Rap1D_WPF.Views.ErrorListView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:Controls="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Docking" xmlns:telerik="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView" mc:Ignorable="d"
d:DesignHeight="74" d:DesignWidth="298" Header="{Binding Header}" DataContextChanged="RadPane_DataContextChanged"]
[Grid]
[telerik:RadGridView AutoGenerateColumns="False" x:Name="grid" ItemsSource="{Binding Path=Messages, NotifyOnSourceUpdated=True}" Margin="0,0,0,37"]
[telerik:RadGridView.Columns]
[telerik:GridViewDataColumn Header="Message" DataMemberBinding="{Binding Path=Message, NotifyOnSourceUpdated=True}" /]
[/telerik:RadGridView.Columns]
[/telerik:RadGridView]
[/Grid]
[/Controls:RadPane]
视图模型:
using System;
using System.Collections.Generic;
using System.Linq;
using Rap1D.ViewModelLayer.Interfaces;
using Rap1D.ViewModelLayer.Interfaces.TreeViewItems;
namespace Rap1D.ViewModelLayer.Implementations
{
public class ErrorListViewModel :ViewModelBase, IErrorListViewModel
{
private readonly List<INotificationMessage> _messages;
public ErrorListViewModel()
{
_messages = new List<INotificationMessage>();
}
public string Header
{
get { return "Error List"; }
}
public IEnumerable<INotificationMessage> Messages
{
get { return _messages; }
}
public void RemoveNotificationsForItem(IProductComponentViewModel productComponentViewModel)
{
var toDelete = (from m in _messages
where m.Item == productComponentViewModel
select m).ToList();
foreach (var notificationMessage in toDelete)
{
_messages.Remove(notificationMessage);
}
OnPropertyChanged("Messages");
}
public void AddNotifications(IProductComponentViewModel productComponentViewModel, IEnumerable<INotificationMessage> list)
{
_messages.AddRange(list);
OnPropertyChanged("Messages");
}
}
}
ErrorManager中:
using System.Collections.Generic;
using Rap1D.ViewModelLayer.Interfaces;
using Rap1D.ViewModelLayer.Interfaces.Managers;
using Rap1D.ViewModelLayer.Interfaces.Providers;
using Rap1D.ViewModelLayer.Interfaces.TreeViewItems;
namespace Rap1D.ViewModelLayer.Implementations.Managers
{
public class ErrorManager : IErrorManager
{
private readonly IErrorListViewModelProvider _errorListViewModelProvider;
public ErrorManager(IErrorListViewModelProvider errorListViewModelProvider)
{
_errorListViewModelProvider = errorListViewModelProvider;
}
public void UpdateNotificationsForItem(IProductComponentViewModel productComponentViewModel,
IEnumerable<INotificationMessage> list)
{
var errorListViewModel = _errorListViewModelProvider.GetViewModel();
errorListViewModel.RemoveNotificationsForItem(productComponentViewModel);
errorListViewModel.AddNotifications(productComponentViewModel, list);
}
}
}
答案 0 :(得分:0)
所有这一切都改为List to ObservableCollection。
有人可以解释为什么我的初始解决方案没有按预期工作吗?
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Microsoft.Practices.Prism;
using Rap1D.ViewModelLayer.Interfaces;
using Rap1D.ViewModelLayer.Interfaces.TreeViewItems;
namespace Rap1D.ViewModelLayer.Implementations
{
public class ErrorListViewModel : ViewModelBase, IErrorListViewModel
{
private readonly ObservableCollection<INotificationMessage> _messages;
public ErrorListViewModel()
{
_messages = new ObservableCollection<INotificationMessage>();
}
public string Header
{
get { return "Error List"; }
}
public void AddNotifications(IProductComponentViewModel productComponentViewModel,
IEnumerable<INotificationMessage> list)
{
_messages.AddRange(list);
OnPropertyChanged("Messages");
}
public IEnumerable<INotificationMessage> Messages
{
get { return _messages; }
}
public void RemoveNotificationsForItem(IProductComponentViewModel productComponentViewModel)
{
var toDelete = (from m in _messages
//where m.Item == productComponentViewModel
select m).ToList();
foreach (var notificationMessage in toDelete)
{
_messages.Remove(notificationMessage);
}
OnPropertyChanged("Messages");
}
}
}
答案 1 :(得分:-1)
ObservableCollections支持PropertyChangeNotification;其他馆藏需要RaisePropertyChanged("ObjectName")
。