INotifyPropertyChanged的不工作的ObservableCollection财产(

2019-10-28 18:27发布

我有一类叫做IssuesView它实现INotifyPropertyChanged 。 此类包含一个ObservableCollection<Issue>并暴露其作为DependencyProperty称为消费问题通过Bindings 。 它被定义为如下 -

    public class IssuesView : DependencyObject, INotifyPropertyChanged
{
    public Issues Issues
    {
        get { return (Issues)GetValue(IssuesProperty); }
        set
        {
            SetValue(IssuesProperty, value);
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("Issues"));
            }
        }
    }

    // Using a DependencyProperty as the backing store for Issues.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty IssuesProperty =
        DependencyProperty.Register("Issues", typeof(Issues), typeof(IssuesView), new UIPropertyMetadata(null));



    public IssuesView()
    {
        Refresh();
    }

    public void Refresh()
    {
        this.Issues = new Issues();
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion
}

我宣布这样的测试页 -

<Page x:Class="Tracker.Pages.DEMO"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:cont="clr-namespace:Tracker.Controls"
Title="DEMO">
<StackPanel>
    <Button Click="Button_Click">Change</Button>
    <cont:IssueTimeline IssuesForTimeline="{Binding Source={StaticResource issuesView},Path = Issues}"/>
</StackPanel>

所述IssuesView类中定义Application.Resources
现在,在事件hadnler的按键我有这样的代码 -

private void Button_Click(object sender, RoutedEventArgs e)
    {
        IssuesView iv = Application.Current.FindResource("issuesView") as IssuesView;
        if (!once)
        {
            foreach (Issue i in iv.Issues)
            {
                i.DormantFor = new TimeSpan(30, 0, 0, 0);
                i.AssignedUserID = 12;
                i.Name = "MyName";
                i.Priority = Issue.Priorities.Critical;
                i.Status = Issue.Statuses.New;
                i.Summary = "NewSummary";
            }
            once = true;
        }
        else
        {
            iv.Refresh();
        }

曾经是一个简单的布尔测试收集与复育的突变。

第一个按钮点击改变了收集的项目,因为项目执行INotifyPropertyChanged的UI正确更新,但第二次点击重新填充集合,但即使该事件不为空和火灾正确不更新UI。

为什么不UI在第二次点击更新? 我怎样才能让这个重新填充集合会导致UI更新?

Answer 1:

你真的需要简化您的摄制。 我可以看到几件事情不对的地方,但不能帮助解决你的问题没有看到这一切 。 这是我简单的摄制,这工作得很好。

Window1.xaml:

<Window x:Name="_root" x:Class="CollectionRepro.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <StackPanel DataContext="{Binding ElementName=_root}">
        <ItemsControl ItemsSource="{Binding Issues}"/>
        <Button x:Name="_addButton">Add</Button>
        <Button x:Name="_resetButton">Reset</Button>
    </StackPanel>
</Window>

Window1.xaml.cs:

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;

namespace CollectionRepro
{
    public partial class Window1 : Window
    {
        public static readonly DependencyProperty IssuesProperty = DependencyProperty.Register("Issues",
            typeof(ICollection<string>),
            typeof(Window1));

        public ICollection<string> Issues
        {
            get { return (ICollection<string>)GetValue(IssuesProperty); }
            set { SetValue(IssuesProperty, value); }
        }

        public Window1()
        {
            InitializeComponent();
            Reset();

            _addButton.Click +=new RoutedEventHandler(_addButton_Click);
            _resetButton.Click += new RoutedEventHandler(_resetButton_Click);
        }

        void  _resetButton_Click(object sender, RoutedEventArgs e)
        {
            Reset();
        }

        void  _addButton_Click(object sender, RoutedEventArgs e)
        {
            Issues.Add("Another issue");
        }

        private void Reset()
        {
            Issues = new ObservableCollection<string>();
        }
    }
}


Answer 2:

首先:没有理由实施INotifyProperty改变DependencyProperties。 DependencyProperties知道他们什么时候改变。

第二:我没有看到ObservableCollection在你的代码。

第三:它不是完全清楚,我(从您发布的代码),从哪儿来,你在第一次点击修改问题。 我从另一个行动承担,这里就不贴了。

我是正确,如果我认为你要清除与第二次点击的问题清单(因为我不知道是什么Issues构造函数)?



文章来源: INotifyPropertyChanged not working on ObservableCollection property