wpf databound RadGrid - почему изменения видовой модели не обнаруживаются сеткой? - PullRequest
0 голосов
/ 26 февраля 2011

Я пытаюсь создать список ошибок, очень похожий на список в Visual Studio.

У меня есть вид и модель вида. Я обновляю модель одноэлементного представления и уведомляю, что в свойстве произошли изменения, которые являются фактическими сообщениями (ошибки и предупреждения) Однако сетка остается неизменной - что не так?!

Я прошел, коллекция _messages обновлена ​​правильно, и начальное сообщение отображается правильно, но остается в поле зрения после обновления модели представления. Другими словами, это должен быть вопрос обновления представления в соответствии с правильной моделью представления.

Мне известно об ошибке в RadPane, из-за которой Панели теряют соединение с моделью представления и реализовали стандартный обходной путь.

Модель представления реализует INotifyPropertyChanged.

(NotifySourceUpdated в xaml добавлен в попытке исправить ситуацию, но безрезультатно)

(скобки заменены в представлении из-за ограничений Stackoverflow)

Спасибо за любой вклад и понимание ...

Андерс, Дания

Вид:

    [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]

ViewModel:

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

Ответы [ 2 ]

0 голосов
/ 13 октября 2014

ObservableCollections поддерживают PropertyChangeNotification; другие коллекции требуют RaisePropertyChanged("ObjectName").

0 голосов
/ 26 февраля 2011

Все, что для этого потребовалось, перейти от Списка к 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");
            }
        }
    }
...