回答編集履歴

1

見直しキャンペーン中

2023/07/29 06:57

投稿

TN8001
TN8001

スコア10108

answer CHANGED
@@ -1,94 +1,94 @@
1
- `ObserveElementProperty`拡張メソッドがあります。
2
-
3
- [ReactiveProperty v2.1.3をリリースしました - かずきのBlog@hatena](https://blog.okazuki.jp/entry/2015/03/10/213610)
4
-
5
- [MVVM をリアクティブプログラミングで快適に ReactiveProperty オーバービュー 2020 年版 後編 - Qiita](https://qiita.com/okazuki/items/6faac7cb1a7d8a6ad0f2#observeelementproperty)
6
-
7
- おそらく効率的な方法があるのでしょうがよくわからなかったので、何か変更があったら全件探索です^^;
8
-
9
- ```xaml
10
- <Window
11
- x:Class="Questions362682.MainWindow"
12
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
13
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
14
- Width="800"
15
- Height="450">
16
- <DockPanel>
17
- <DockPanel DockPanel.Dock="Top">
18
- <Button Command="{Binding AddCommand}" Content="Add" />
19
- <TextBlock Text="{Binding IsLargeLength.Value}" />
20
- </DockPanel>
21
- <ItemsControl ItemsSource="{Binding Items}">
22
- <ItemsControl.ItemTemplate>
23
- <DataTemplate>
24
- <DockPanel>
25
- <Button
26
- Command="{Binding DataContext.DellCommand, RelativeSource={RelativeSource FindAncestor, AncestorType=ItemsControl}}"
27
- CommandParameter="{Binding}"
28
- Content="Del" />
29
- <TextBox Text="{Binding Id, UpdateSourceTrigger=PropertyChanged}" />
30
- </DockPanel>
31
- </DataTemplate>
32
- </ItemsControl.ItemTemplate>
33
- </ItemsControl>
34
- </DockPanel>
35
- </Window>
36
- ```
37
-
38
- ```C#
39
- using Prism.Commands;
40
- using Prism.Mvvm;
41
- using Reactive.Bindings;
42
- using Reactive.Bindings.Extensions;
43
- using System.Collections.ObjectModel;
44
- using System.Linq;
45
- using System.Reactive.Linq;
46
- using System.Windows;
47
-
48
- namespace Questions362682
49
- {
50
- public class MyData : BindableBase
51
- {
52
- public string Id { get => _id; set => SetProperty(ref _id, value); }
53
- private string _id;
54
- }
55
-
56
- public class MyDataUserViewModel
57
- {
58
- public ObservableCollection<MyData> Items { get; }
59
- public ReadOnlyReactiveProperty<bool> IsLargeLength { get; }
60
-
61
- public DelegateCommand AddCommand { get; }
62
- public DelegateCommand<MyData> DellCommand { get; }
63
-
64
- private int i;
65
-
66
- public MyDataUserViewModel()
67
- {
68
- Items = new(Enumerable.Range(1, 10).Select(_ => new MyData { Id = $"{i++}", }));
69
-
70
- AddCommand = new(() => Items.Add(new() { Id = $"{i++}", }));
71
- DellCommand = new(x => Items.Remove(x));
72
-
73
- var a = Items.ObserveElementProperty(x => x.Id)
74
- .Select(_ => Items.Any(x => x.Id.Length >= 2));
75
- var b = Items.CollectionChangedAsObservable()
76
- .Select(_ => Items.Any(x => x.Id.Length >= 2));
77
- IsLargeLength = a.Merge(b).ToReadOnlyReactiveProperty();
78
- }
79
- }
80
-
81
- public partial class MainWindow : Window
82
- {
83
- public MainWindow()
84
- {
85
- InitializeComponent();
86
- DataContext = new MyDataUserViewModel();
87
- }
88
- }
89
- }
90
- ```
91
-
92
- ---
93
-
1
+ `ObserveElementProperty`拡張メソッドがあります。
2
+
3
+ [ReactiveProperty v2.1.3をリリースしました - かずきのBlog@hatena](https://blog.okazuki.jp/entry/2015/03/10/213610)
4
+
5
+ [MVVM をリアクティブプログラミングで快適に ReactiveProperty オーバービュー 2020 年版 後編 - Qiita](https://qiita.com/okazuki/items/6faac7cb1a7d8a6ad0f2#observeelementproperty)
6
+
7
+ おそらく効率的な方法があるのでしょうがよくわからなかったので、何か変更があったら全件探索です^^;
8
+
9
+ ```xml
10
+ <Window
11
+ x:Class="Questions362682.MainWindow"
12
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
13
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
14
+ Width="800"
15
+ Height="450">
16
+ <DockPanel>
17
+ <DockPanel DockPanel.Dock="Top">
18
+ <Button Command="{Binding AddCommand}" Content="Add" />
19
+ <TextBlock Text="{Binding IsLargeLength.Value}" />
20
+ </DockPanel>
21
+ <ItemsControl ItemsSource="{Binding Items}">
22
+ <ItemsControl.ItemTemplate>
23
+ <DataTemplate>
24
+ <DockPanel>
25
+ <Button
26
+ Command="{Binding DataContext.DellCommand, RelativeSource={RelativeSource FindAncestor, AncestorType=ItemsControl}}"
27
+ CommandParameter="{Binding}"
28
+ Content="Del" />
29
+ <TextBox Text="{Binding Id, UpdateSourceTrigger=PropertyChanged}" />
30
+ </DockPanel>
31
+ </DataTemplate>
32
+ </ItemsControl.ItemTemplate>
33
+ </ItemsControl>
34
+ </DockPanel>
35
+ </Window>
36
+ ```
37
+
38
+ ```cs
39
+ using Prism.Commands;
40
+ using Prism.Mvvm;
41
+ using Reactive.Bindings;
42
+ using Reactive.Bindings.Extensions;
43
+ using System.Collections.ObjectModel;
44
+ using System.Linq;
45
+ using System.Reactive.Linq;
46
+ using System.Windows;
47
+
48
+ namespace Questions362682
49
+ {
50
+ public class MyData : BindableBase
51
+ {
52
+ public string Id { get => _id; set => SetProperty(ref _id, value); }
53
+ private string _id;
54
+ }
55
+
56
+ public class MyDataUserViewModel
57
+ {
58
+ public ObservableCollection<MyData> Items { get; }
59
+ public ReadOnlyReactiveProperty<bool> IsLargeLength { get; }
60
+
61
+ public DelegateCommand AddCommand { get; }
62
+ public DelegateCommand<MyData> DellCommand { get; }
63
+
64
+ private int i;
65
+
66
+ public MyDataUserViewModel()
67
+ {
68
+ Items = new(Enumerable.Range(1, 10).Select(_ => new MyData { Id = $"{i++}", }));
69
+
70
+ AddCommand = new(() => Items.Add(new() { Id = $"{i++}", }));
71
+ DellCommand = new(x => Items.Remove(x));
72
+
73
+ var a = Items.ObserveElementProperty(x => x.Id)
74
+ .Select(_ => Items.Any(x => x.Id.Length >= 2));
75
+ var b = Items.CollectionChangedAsObservable()
76
+ .Select(_ => Items.Any(x => x.Id.Length >= 2));
77
+ IsLargeLength = a.Merge(b).ToReadOnlyReactiveProperty();
78
+ }
79
+ }
80
+
81
+ public partial class MainWindow : Window
82
+ {
83
+ public MainWindow()
84
+ {
85
+ InitializeComponent();
86
+ DataContext = new MyDataUserViewModel();
87
+ }
88
+ }
89
+ }
90
+ ```
91
+
92
+ ---
93
+
94
94
  `IFilteredReadOnlyObservableCollection`とかも使えるかな?と思ったんのですが、`Count`を発砲してくれませんでした。。。