2009-02-27 11 views
0

INotifyPropertyChangedを実装するIssuesViewというクラスがあります。このクラスはObservableCollection<Issue>を保持し、Bindingsによって消費の問題と呼ばれるDependencyPropertyとして公開します。 IssuesViewクラスがApplication.Resourcesで定義されているInotifyPropertyChangedがObservableCollectionプロパティで機能していません

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

- 私はこのように宣言テストページを持っている

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 
} 

- 以下のように定義されます。今、私はこのコードを持っているボタンのイベント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を実装するため、最初のボタンクリックでコレクションのアイテムが変更されますが、2回目のクリックでコレクションが再読み込みされますが、イベントがnullでなくてもUIは更新されません。

2回目のクリックでUIが更新されないのはなぜですか?コレクションの再投入によってUIが更新されるようにするにはどうすればよいですか?

+0

なぜ、このコミュニティのwikiがありますか? –

+0

Ja、applogies。それは間違いだった。あなたの答えをありがとう、本当に助けて! – Stimul8d

答えて

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

最初に、INotifyPropertyをDependencyPropertiesに変更する理由はありません。 DependencyPropertiesは、変更された時点を認識します。

2番目:コードにObservableCollectionが表示されません。

第3回:あなたが最初にクリックしたときに変更した問題がどこから来たのか(あなたが投稿したコードから)はっきりとわかりません。私はここに掲載されていない別のアクションから推測します。

Issuesコンストラクタが何をしているのかわからないので、2番目のクリックで問題リストをクリアしたいと思ったら正解でしょうか?