5

ユーザーが DataGrid のセルのコンテンツを編集するたびに知りたいと思っています。CellEditEnding イベントがありますが、DataGrid がバインドされているコレクションに変更が加えられる前に呼び出されます。

私のデータグリッドは、WCF mex エンドポイントから自動的に生成されたクラスであるObservableCollection<Item>にバインドされています。Item

ユーザーがコレクションへの変更をコミットするたびに知る最善の方法は何ですか。

アップデート

CollectionChanged イベントを試しましたが、Item変更されたときにトリガーされません。

4

4 に答える 4

9

UpdateSourceTrigger=PropertyChangedデータグリッドのプロパティ メンバーのバインディングで使用できます。これにより、CellEditEnding が起動されたときに、更新が監視可能なコレクションに既に反映されていることが保証されます。

下記参照

<DataGrid SelectionMode="Single"
          AutoGenerateColumns="False"
          CanUserAddRows="False"
          ItemsSource="{Binding Path=Items}" // This is your ObservableCollection
          SelectedIndex="{Binding SelectedIndexStory}">
          <e:Interaction.Triggers>
              <e:EventTrigger EventName="CellEditEnding">
                 <cmd:EventToCommand PassEventArgsToCommand="True" Command="{Binding EditStoryCommand}"/> // Mvvm light relay command
               </e:EventTrigger>
          </e:Interaction.Triggers>
          <DataGrid.Columns>
                    <DataGridTextColumn Header="Description"
                        Binding="{Binding Name, UpdateSourceTrigger=PropertyChanged}" /> // Name is property on the object i.e Items.Name
          </DataGrid.Columns>

</DataGrid>

UpdateSourceTrigger = PropertyChanged は、ターゲット プロパティが変更されるとすぐにプロパティ ソースを変更します。

イベント ハンドラーを監視可能なコレクションの変更されたイベントに追加しても、コレクション内のオブジェクトの編集に対しては発生しないため、これによりアイテムへの編集をキャプチャできます。

于 2014-12-01T23:23:51.233 に答える
0

編集された DataGrid アイテムが特定のコレクションに属しているかどうかを知る必要がある場合は、DataGrid の RowEditEnding イベントで次のようにすることができます。

    private void dg_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
    {
        // dg is the DataGrid in the view
        object o = dg.ItemContainerGenerator.ItemFromContainer(e.Row);

        // myColl is the observable collection
        if (myColl.Contains(o)) { /* item in the collection was updated! */  }
    }
于 2012-04-30T19:10:01.427 に答える
-1

ObservableCollectionのイベントにイベントハンドラーを追加するだけCollectionChangedです。

コードスニペット:

_listObsComponents.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(ListCollectionChanged);

// ...


    void ListCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        /// Work on e.Action here (can be Add, Move, Replace...)
    }

の場合、これe.ActionReplaceリストのオブジェクトが置き換えられたことを意味します。もちろん、このイベントは変更が適用された後にトリガーされます

楽しむ!

于 2012-04-30T18:16:04.957 に答える