前提・実現したいこと
WPFのDataGridにItemsSourceでObservableCollectionを指定してます。
ObservableCollectionに要素が追加されたことをDataGridのイベント等で検知して処理を行いたい。
試したこと
SourceUpdated,AddingNewItem,RowEditEndingを試したが検知しなかった
補足情報
.NET 6.0
気になる質問をクリップする
クリップした質問は、後からいつでもマイページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
下記のような質問は推奨されていません。
- 質問になっていない投稿
- スパムや攻撃的な表現を用いた投稿
適切な質問に修正を依頼しましょう。
2022/10/28 10:54
回答1件
2
ObservableCollectionに要素が追加されたことをDataGridのイベント等で検知して処理を行いたい。
DataGrid
のイベントは主にユーザー操作で発砲するもので、ItemsSource
のイベント(CollectionChanged
等)をわざわざ中継しないでしょう(DataGrid
が購読する側です)
単にObservableCollection
のCollectionChanged
でいいのでは?
ObservableCollection<T>.CollectionChanged イベント (System.Collections.ObjectModel) | Microsoft Learn
あるいはCollectionView
のCollectionChanged
でもいいでしょう。
CollectionView.CollectionChanged イベント (System.Windows.Data) | Microsoft Learn
方法: DataGrid コントロールでデータをグループ化、並べ替え、およびフィルター処理する - WPF .NET Framework | Microsoft Learn
xml
1<Window 2 x:Class="Q2ckyz891emznjk.MainWindow" 3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 5 xmlns:core="clr-namespace:System;assembly=mscorlib" 6 xmlns:local="clr-namespace:Q2ckyz891emznjk" 7 Width="800" 8 Height="450"> 9 <Window.Resources> 10 <ObjectDataProvider 11 x:Key="myEnum" 12 MethodName="GetValues" 13 ObjectType="{x:Type core:Enum}"> 14 <ObjectDataProvider.MethodParameters> 15 <x:Type Type="local:OrderStatus" /> 16 </ObjectDataProvider.MethodParameters> 17 </ObjectDataProvider> 18 </Window.Resources> 19 <DockPanel> 20 <Button 21 Click="AddButton_Click" 22 Content="Add" 23 DockPanel.Dock="Top" /> 24 <Button 25 Click="RemoveButton_Click" 26 Content="Remove" 27 DockPanel.Dock="Top" /> 28 <DataGrid 29 Name="dataGrid" 30 AutoGenerateColumns="False" 31 ItemsSource="{Binding Customers}"> 32 <DataGrid.Columns> 33 <DataGridTextColumn Binding="{Binding FirstName}" Header="First Name" /> 34 <DataGridTextColumn Binding="{Binding LastName}" Header="Last Name" /> 35 <DataGridCheckBoxColumn Binding="{Binding IsMember}" Header="Member?" /> 36 <DataGridComboBoxColumn 37 Header="Order Status" 38 ItemsSource="{Binding Source={StaticResource myEnum}}" 39 SelectedItemBinding="{Binding Status}" /> 40 </DataGrid.Columns> 41 </DataGrid> 42 </DockPanel> 43</Window>
cs
1using System.Collections.ObjectModel; 2using System.Collections.Specialized; 3using System.Diagnostics; 4using System.Windows; 5using System.Windows.Data; 6 7namespace Q2ckyz891emznjk; 8 9 10public enum OrderStatus { None, New, Processing, Shipped, Received }; 11 12public record class Customer 13{ 14 public string? FirstName { get; set; } 15 public string? LastName { get; set; } 16 public bool IsMember { get; set; } 17 public OrderStatus Status { get; set; } 18} 19 20public partial class MainWindow : Window 21{ 22 public ObservableCollection<Customer> Customers { get; } = new() 23 { 24 new() { FirstName = "foo", LastName = "bar", }, 25 new() { FirstName = "hoge", LastName = "piyo", }, 26 }; 27 28 public MainWindow() 29 { 30 DataContext = this; 31 InitializeComponent(); 32 33 Customers.CollectionChanged += Customers_CollectionChanged; 34 35 36 var collectionView = CollectionViewSource.GetDefaultView(dataGrid.ItemsSource); 37 collectionView.CollectionChanged += CollectionView_CollectionChanged; 38 } 39 40 private void Customers_CollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) 41 { 42 Debug.WriteLine("\nCustomers_CollectionChanged"); 43 44 if (e.NewItems != null) 45 { 46 Debug.WriteLine("add"); 47 foreach (var item in e.NewItems) 48 { 49 Debug.WriteLine(item); 50 } 51 } 52 53 if (e.OldItems != null) 54 { 55 Debug.WriteLine("remove"); 56 foreach (var item in e.OldItems) 57 { 58 Debug.WriteLine(item); 59 } 60 } 61 } 62 63 private void CollectionView_CollectionChanged(object? sender, NotifyCollectionChangedEventArgs e) 64 { 65 Debug.WriteLine("\nCollectionView_CollectionChanged"); 66 67 if (e.NewItems != null) 68 { 69 Debug.WriteLine("add"); 70 foreach (var item in e.NewItems) 71 { 72 Debug.WriteLine(item); 73 } 74 } 75 76 if (e.OldItems != null) 77 { 78 Debug.WriteLine("remove"); 79 foreach (var item in e.OldItems) 80 { 81 Debug.WriteLine(item); 82 } 83 } 84 } 85 86 private void AddButton_Click(object sender, RoutedEventArgs e) 87 { 88 Customers.Add(new() { FirstName = "FirstName", LastName = "LastName", }); 89 } 90 91 private void RemoveButton_Click(object sender, RoutedEventArgs e) 92 { 93 if (dataGrid.SelectedItem is Customer customer) 94 { 95 Customers.Remove(customer); 96 } 97 } 98}
投稿2022/10/08 16:38
総合スコア8292
下記のような回答は推奨されていません。
- 質問の回答になっていない投稿
- スパムや攻撃的な表現を用いた投稿
このような回答には修正を依頼しましょう。
関連した質問
Q&A
解決済
WPFのMVVMフレームワークでディスプレイキーボードを実装する際に、SendWaitがうまく機能しない件について
回答1
クリップ1
更新
2023/03/17
Q&A
解決済
WPFでDataGridのComboBoxへのBinding方法
回答2
クリップ1
更新
2020/01/08
Q&A
解決済
データグリッドでラジオボタンが選択した行を取得したい
回答1
クリップ1
更新
2023/03/03
Q&A
解決済
WPFのデータグリッドで、項目が増えてしまいます
回答3
クリップ0
更新
2022/11/17
Q&A
解決済
(WPF)ー動的に生成したImageにContextMenuをつける方法
回答2
クリップ1
更新
2023/03/14
Q&A
解決済
新規開発ソフトのWindowsForms、WPF、UWPの選択について
回答5
クリップ7
更新
2017/05/21
Q&A
解決済
ObservableCollectionの要素クラスのプロパティ
回答1
クリップ0
更新
2023/03/22
Q&A
解決済
WPFのWindow.VisibilityのBindingが動作していないように見える
回答1
クリップ1
更新
2023/03/14
同じタグがついた質問を見る
C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。
Microsoft Visual StudioはMicrosoftによる統合開発環境(IDE)です。多種多様なプログラミング言語に対応しています。
XAML(Extensible Application Markup Language)はWPF、Silverlight、Windows PhoneそしてWindows Store appsでユーザーインターフェースを定義するために使われるXML言語です。
Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです