0

我有一个名为IssuesViewwhich implements的类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();
        }

once 是一个简单的布尔值,用于测试集合的突变与重新填充。

第一次单击按钮会更改集合的项,并且 UI 会正确更新,因为这些项实现了 INotifyPropertyChanged,但第二次单击会重新填充集合,但不会更新 UI,即使事件不为 null 并且正确触发也是如此。

为什么第二次点击时 UI 不更新?我怎样才能使它重新填充集合将导致 UI 更新?

4

2 回答 2

2

首先:没有理由INotifyProperty为 DependencyProperties 实施更改。DependencyProperties 知道它们何时更改。

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

第三:我并不完全清楚(从您发布的代码中)您在第一次点击中修改的问题来自哪里。我从另一个动作中假设,未在此处发布。

如果我假设您想通过第二次单击清除问题列表,我是否正确(因为我不知道Issues构造函数的作用)?

于 2009-02-27T12:59:47.533 回答
2

你真的需要简化你的复制。我可以看到它有几个问题,但是如果没有看到所有问题,就无法帮助解决您的问题。这是我的简单复制品,效果很好。

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>();
        }
    }
}
于 2009-02-27T13:00:56.347 回答