前提・実現したいこと
WPFのDataGridにItemsSourceでObservableCollectionを指定してます。
ObservableCollectionに要素が追加されたことをDataGridのイベント等で検知して処理を行いたい。
試したこと
SourceUpdated,AddingNewItem,RowEditEndingを試したが検知しなかった
補足情報
.NET 6.0
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/10/28 10:54

回答1件
0
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
総合スコア10022
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。