5

がバインドされRowDetailsTemplateているコレクション (「アイテム」) を変更するときに、 を更新するのに問題があります。DataGridコレクションは、ビュー モデル内から変更されています。バインドされたアイテムの 1 つのコンテンツを変更すると、DataGridRow と RowDetailsTemplate の両方で変更が更新されます。例えば

Items[i].Name = "new name";  // RowDetailsTemplate gets updated

しかし、項目の 1 つをまったく新しいオブジェクトに割り当てると、DataGridRow は更新されましたが、RowDetailsTemplate は更新されません。例えば

Items[i] = new Model {Name = "new name"};  // RowDetailsTemplate NOT updated

最初に考えたのは、バインドされたアイテムの CollectionChanged イベントにリスナーを追加し、プロパティの変更通知を明示的に発生させる必要があるということだけでした。例えば

Items = new ObeservableCollection<Model>();
Items.CollectionChanged += (o,e) => OnNotifyPropertyChanged("Items");

しかし、それはうまくいきませんでした。

私の XAML バインディングは次のようになります。

<DataGrid DataContext="{StaticResource viewmodel}" 
          ItemsSource="{Binding Items, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnTargetUpdated=True, NotifyOnSourceUpdated=True}">
  <DataGrid.RowDetailsTemplate>
    <DataTemplate>
      <TextBlock Text="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnTargetUpdated=True, NotifyOnSourceUpdated=True}"/>
    </DataTemplate>
  </DataGrid.RowDetailsTemplate>
</DataGrid>

DataGridRow変更されたアイテムが通知されるのに、なぜ通知されないのRowDetailsTemplateですか?!

更新 コレクションを変更する代わりに、削除/追加を行います。例えば

Items.Remove(Items[i]);
Items.Add (new Model {Name = "new name"});  // RowDetailsTemplate updated OK

(もちろん、Model クラスは を実装してINotifyPropertyChangedいます。)

これは、詳細ビューの DataContext を更新する必要がある問題のように思えますか?

4

2 に答える 2

2

できない理由:

Items.RemoveAt(i);
Items.Insert(i,(new Model {Name = "new name"});

同じ効果があります。

于 2012-11-07T21:01:03.433 に答える
1

CellEditEnding ハンドラー コードに次のような汚いハックを挿入する必要がありました。

DataTemplate temp = ProfileDataGrid.RowDetailsTemplate;
ProfileDataGrid.RowDetailsTemplate = null;
ProfileDataGrid.RowDetailsTemplate = temp;

それは機能し、Row Detail が更新されますが、マスターが RowDetails を更新する方法も知りたいです。

于 2015-02-26T13:22:06.677 に答える