teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

見直しキャンペーン中

2023/07/28 17:03

投稿

TN8001
TN8001

スコア10111

answer CHANGED
@@ -1,75 +1,75 @@
1
- C#コード側から`DataInfo`のプロパティを変更する場合は、`DataInfo`に`INotifyPropertyChanged`を実装してください。
2
-
3
- WPFにおいて`INotifyPropertyChanged`は最重要インターフェースです。
4
- [INotifyPropertyChanged インターフェイス (System.ComponentModel) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.componentmodel.inotifypropertychanged)
5
-
6
- `vmDataInfo`の`get`・`set`でやっていることを、`SelectIndex`でもやるということです(逆に`vmDataInfo`でやる意味はないです)
7
-
8
- ```C#
9
- using Livet;
10
- using System.Collections.ObjectModel;
11
-
12
- namespace Questions355938.ViewModels
13
- {
14
- public class DataInfo : ViewModel
15
- {
16
- private string _SelectIndex;
17
- public string SelectIndex
18
- {
19
- get => _SelectIndex;
20
- set
21
- {
22
- //if (_SelectIndex != value)
23
- //{
24
- // _SelectIndex = value;
25
- // RaisePropertyChanged();
26
- //}
27
- // ↑の短縮形
28
- RaisePropertyChangedIfSet(ref _SelectIndex, value);
29
- }
30
- }
31
-
32
- public string ParamName { get; set; }
33
- public int Param1 { get; set; }
34
- public int Param2 { get; set; }
35
- }
36
-
37
- class MainWindowVM : ViewModel
38
- {
39
- public ObservableCollection<DataInfo> vmDataInfo { get; }
40
-
41
- public MainWindowVM()
42
- {
43
- vmDataInfo = new ObservableCollection<DataInfo>
44
- {
45
- new DataInfo { ParamName = "Parameter 1", Param1 = 100, Param2 = 0 },
46
- new DataInfo { ParamName = "Parameter 2", Param1 = 200, Param2 = 99 },
47
- new DataInfo { ParamName = "Parameter 3", Param1 = 300, Param2 = 199 },
48
- new DataInfo { ParamName = "Parameter 4", Param1 = 400, Param2 = 299 },
49
- new DataInfo { ParamName = "Parameter 5", Param1 = 500, Param2 = 399 },
50
- };
51
- }
52
-
53
- public void CurrentCellChanged(object sender)
54
- {
55
- if (sender is DataInfo info)
56
- {
57
- var selectIndex = 0;
58
- foreach (var data in vmDataInfo)
59
- {
60
- if (info.ParamName == data.ParamName)
61
- {
62
- vmDataInfo[selectIndex].SelectIndex = "●";
63
- }
64
- else
65
- {
66
- vmDataInfo[selectIndex].SelectIndex = "";
67
- }
68
-
69
- selectIndex++;
70
- }
71
- }
72
- }
73
- }
74
- }
1
+ C#コード側から`DataInfo`のプロパティを変更する場合は、`DataInfo`に`INotifyPropertyChanged`を実装してください。
2
+
3
+ WPFにおいて`INotifyPropertyChanged`は最重要インターフェースです。
4
+ [INotifyPropertyChanged インターフェイス (System.ComponentModel) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.componentmodel.inotifypropertychanged)
5
+
6
+ `vmDataInfo`の`get`・`set`でやっていることを、`SelectIndex`でもやるということです(逆に`vmDataInfo`でやる意味はないです)
7
+
8
+ ```cs
9
+ using Livet;
10
+ using System.Collections.ObjectModel;
11
+
12
+ namespace Questions355938.ViewModels
13
+ {
14
+ public class DataInfo : ViewModel
15
+ {
16
+ private string _SelectIndex;
17
+ public string SelectIndex
18
+ {
19
+ get => _SelectIndex;
20
+ set
21
+ {
22
+ //if (_SelectIndex != value)
23
+ //{
24
+ // _SelectIndex = value;
25
+ // RaisePropertyChanged();
26
+ //}
27
+ // ↑の短縮形
28
+ RaisePropertyChangedIfSet(ref _SelectIndex, value);
29
+ }
30
+ }
31
+
32
+ public string ParamName { get; set; }
33
+ public int Param1 { get; set; }
34
+ public int Param2 { get; set; }
35
+ }
36
+
37
+ class MainWindowVM : ViewModel
38
+ {
39
+ public ObservableCollection<DataInfo> vmDataInfo { get; }
40
+
41
+ public MainWindowVM()
42
+ {
43
+ vmDataInfo = new ObservableCollection<DataInfo>
44
+ {
45
+ new DataInfo { ParamName = "Parameter 1", Param1 = 100, Param2 = 0 },
46
+ new DataInfo { ParamName = "Parameter 2", Param1 = 200, Param2 = 99 },
47
+ new DataInfo { ParamName = "Parameter 3", Param1 = 300, Param2 = 199 },
48
+ new DataInfo { ParamName = "Parameter 4", Param1 = 400, Param2 = 299 },
49
+ new DataInfo { ParamName = "Parameter 5", Param1 = 500, Param2 = 399 },
50
+ };
51
+ }
52
+
53
+ public void CurrentCellChanged(object sender)
54
+ {
55
+ if (sender is DataInfo info)
56
+ {
57
+ var selectIndex = 0;
58
+ foreach (var data in vmDataInfo)
59
+ {
60
+ if (info.ParamName == data.ParamName)
61
+ {
62
+ vmDataInfo[selectIndex].SelectIndex = "●";
63
+ }
64
+ else
65
+ {
66
+ vmDataInfo[selectIndex].SelectIndex = "";
67
+ }
68
+
69
+ selectIndex++;
70
+ }
71
+ }
72
+ }
73
+ }
74
+ }
75
75
  ```