0

INPCを実装する2つの文字列プロパティを持つ単純なクラスがあるとします。

 public class TestClass : INotifyPropertyChanged
    {
        private string _category;
        public string Category
        {
            get { return _category; }
            set { _category = value; NotifyPropertyChanged("Category"); }
        }

        private string _name;
        public string Name
        {
            get { return _name; }
            set { _name = value; NotifyPropertyChanged("Name"); }
        }

        private void NotifyPropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }

これらのObservableCollectionを作成し、PagedCollectionViewに割り当てましょう。

public PagedCollectionView MyItems { get; set; }

public void Test()
{
    var items = new ObservableCollection<TestClass>();
    items.Add(new TestClass { Category = "A", Name = "Item 1" });
    items.Add(new TestClass { Category = "A", Name = "Item 2" });
    items.Add(new TestClass { Category = "B", Name = "Item 3" });
    items.Add(new TestClass { Category = "B", Name = "Item 4" });
    items.Add(new TestClass { Category = "C", Name = "Item 5" });
    items.Add(new TestClass { Category = "C", Name = "Item 6" });
    items.Add(new TestClass { Category = "C", Name = "Item 7" });

    MyItems = new PagedCollectionView(items);
    MyItems.GroupDescriptions.Add(new PropertyGroupDescription("Category"));
}
  • カテゴリにグループ化も設定していることに注意してください

次に、MVVMを使用して、これをXAMLにバインドします。

<sdk:DataGrid ItemsSource="{Binding Path=MyItems}" />

次に、カテゴリの1つを編集して、たとえば「C」の1つを「A」に変更すると、gridviewはそれを見事に処理します。グループの折りたたみステータスを維持し、必要に応じて新しいグループヘッダーを追加します。

この問題は、ビューモデルでプログラムによって(または、同じデータにバインドされた別のグリッドビュー内から)カテゴリを変更したときに発生します。この場合、カテゴリテキストは更新されますが、アイテムは新しい適切なグループに移動せず、新しいグループは作成されず、行ヘッダーは更新されません。

グリッドビュー編集機能の外部でプロパティが変更されたときに、グリッドビューをトリガーしてグループを更新するにはどうすればよいですか?

あらゆる回避策を歓迎しますが、単純なRefresh()をトリガーしても、スクロール/折りたたみなどが消去されるわけではありません。

4

1 に答える 1

1

EditItem()とCommitEdit()で編集をラップする必要があります

        // The following will fail to regroup
        //(MyItems[3] as TestClass).Category = "D";

        // The following works
        MyItems.EditItem(MyItems[3]);
        (MyItems[3] as TestClass).Category = "D";
        MyItems.CommitEdit();

        // The following will also fail to regroup            
        //(MyItems[3] as TestClass).Category = "D";
        //items[3] = items[3];

        // fails as well, surprisingly
        //(MyItems[3] as TestClass).Category = "D";
        //TestClass tmp = items[3];
        //items.RemoveAt(3);
        //items.Insert(3, tmp);
于 2012-06-03T22:53:17.637 に答える