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

回答編集履歴

2

見直しキャンペーン中

2023/08/15 10:12

投稿

TN8001
TN8001

スコア10114

answer CHANGED
@@ -2,24 +2,23 @@
2
2
  > ViewModel側でStackPanelを保持したいです。
3
3
 
4
4
  MVVMではWindows Forms的な考え方は捨ててください。
5
- 「Windows Forms的な考え方」とは、
5
+ 「Windows Forms的な考え方」とは、↓のようなことです。
6
6
  > コードビハインドでは「spanel.Children.Add(textBlock);」のように書く
7
7
 
8
- のようなことです。
9
8
  まったく手法が違うので最初は頭の切り替えが大変なのですが、ここがWPFの肝ですし頑張ってください(WPFでもWindows Forms的にコードビハインドでガリガリ書くのを否定するつもりはありません)
10
9
 
11
10
  MVVMについては良記事がたくさんありますので、検索してください^^;
12
11
 
13
12
  ---
14
13
 
15
- 今回の目標は「StackPanelに動的にTextBlockを追加」ということですね。
14
+ 今回の目標は「`StackPanel`に動的に`TextBlock`を追加」ということですね。
16
15
 
17
16
  「`StackPanel`」ということは、アイテムが複数あるということです。
18
17
  そして「動的」ということは、アイテムの増減があるということです。
19
18
 
20
19
  この時点で、
21
- * `View`では`ItemsControl`(`ListBox`や`DataGrid`等も含む)を使う
20
+ * Viewでは`ItemsControl`(`ListBox`や`DataGrid`等も含む)を使う
22
- * `ViewModel`では`ObservableCollection`を使う
21
+ * ViewModelでは`ObservableCollection`を使う
23
22
 
24
23
  ことが確定します(もうそういうもんだと覚えてください^^;
25
24
 
@@ -27,7 +26,7 @@
27
26
 
28
27
  [ObservableCollection<T> クラス (System.Collections.ObjectModel) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.collections.objectmodel.observablecollection-1?view=net-6.0)
29
28
 
30
- コードを見てもらったほうが早いですが、`ViewModel`では文字列のコレクションとして保持し、`View`でその文字列を`DataTemplate`で`TextBlock.Run`に展開する感じです^^
29
+ コードを見てもらったほうが早いですが、ViewModelでは文字列のコレクションとして保持し、Viewでその文字列を`DataTemplate`で`TextBlock.Run`に展開する感じです^^
31
30
 
32
31
 
33
32
  ```xml

1

見直しキャンペーン中

2023/07/29 13:59

投稿

TN8001
TN8001

スコア10114

answer CHANGED
@@ -1,118 +1,118 @@
1
- > StackPanelに動的にTextBlockを追加するため
2
- > ViewModel側でStackPanelを保持したいです。
3
-
4
- MVVMではWindows Forms的な考え方は捨ててください。
5
- 「Windows Forms的な考え方」とは、
6
- > コードビハインドでは「spanel.Children.Add(textBlock);」のように書く
7
-
8
- のようなことです。
9
- まったく手法が違うので最初は頭の切り替えが大変なのですが、ここがWPFの肝ですし頑張ってください(WPFでもWindows Forms的にコードビハインドでガリガリ書くのを否定するつもりはありません)
10
-
11
- MVVMについては良記事がたくさんありますので、検索してください^^;
12
-
13
- ---
14
-
15
- 今回の目標は「StackPanelに動的にTextBlockを追加」ということですね。
16
-
17
- 「`StackPanel`」ということは、アイテムが複数あるということです。
18
- そして「動的」ということは、アイテムの増減があるということです。
19
-
20
- この時点で、
21
- * `View`では`ItemsControl`(`ListBox`や`DataGrid`等も含む)を使う
22
- * `ViewModel`では`ObservableCollection`を使う
23
-
24
- ことが確定します(もうそういうもんだと覚えてください^^;
25
-
26
- [ItemsControl 攻略 ~ 外観のカスタマイズ | grabacr.nét](http://grabacr.net/archives/1240)
27
-
28
- [ObservableCollection<T> クラス (System.Collections.ObjectModel) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.collections.objectmodel.observablecollection-1?view=net-6.0)
29
-
30
- コードを見てもらったほうが早いですが、`ViewModel`では文字列のコレクションとして保持し、`View`でその文字列を`DataTemplate`で`TextBlock.Run`に展開する感じです^^
31
-
32
-
33
- ```xaml
34
- <Window
35
- x:Class="Questions376475.MainWindow"
36
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
37
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
38
- xmlns:local="clr-namespace:Questions376475"
39
- Width="525"
40
- Height="350">
41
- <Window.DataContext>
42
- <local:ViewModel />
43
- </Window.DataContext>
44
- <DockPanel>
45
- <Label Content="{Binding TestLabel}" DockPanel.Dock="Top" />
46
-
47
- <Button
48
- Command="{Binding AddCommand}"
49
- Content="Add"
50
- DockPanel.Dock="Bottom" />
51
-
52
- <ItemsControl ItemsSource="{Binding ItemList}">
53
- <ItemsControl.ItemTemplate>
54
- <DataTemplate>
55
- <TextBlock>
56
- <Run Foreground="Red" Text="{Binding .}" />
57
- </TextBlock>
58
- <!--<TextBlock Foreground="Red" Text="{Binding .}" />-->
59
- </DataTemplate>
60
- </ItemsControl.ItemTemplate>
61
- </ItemsControl>
62
- </DockPanel>
63
- </Window>
64
- ```
65
-
66
- ```C#
67
- using System.Collections.ObjectModel;
68
- using System.Windows;
69
- using CommunityToolkit.Mvvm.ComponentModel;
70
- using CommunityToolkit.Mvvm.Input;
71
-
72
- namespace Questions376475
73
- {
74
- public class ViewModel : ObservableObject // ViewModelBase
75
- {
76
- private string _testLabel = "初期値";
77
- public string TestLabel
78
- {
79
- get => _testLabel;
80
- set => SetProperty(ref _testLabel, value);
81
- }
82
-
83
- public ObservableCollection<string> ItemList { get; }
84
-
85
- public RelayCommand AddCommand { get; }
86
-
87
-
88
- public ViewModel()
89
- {
90
- ItemList = new ObservableCollection<string>
91
- {
92
- "テスト1",
93
- "テスト2",
94
- "テスト3",
95
- };
96
-
97
- AddCommand = new RelayCommand(Add);
98
- }
99
-
100
- private void Add()
101
- {
102
- ItemList.Add($"テスト{ItemList.Count + 1}");
103
-
104
- TestLabel = $"{ItemList.Count}個です";
105
- }
106
- }
107
-
108
- public partial class MainWindow : Window
109
- {
110
- public MainWindow() => InitializeComponent();
111
- }
112
- }
113
- ```
114
-
115
- `ViewModelBase`や`ICommand`実装を書くのがだるいので、下記を使用しました。
116
- [NuGet Gallery | CommunityToolkit.Mvvm 7.1.2](https://www.nuget.org/packages/CommunityToolkit.Mvvm/7.1.2)
117
-
1
+ > StackPanelに動的にTextBlockを追加するため
2
+ > ViewModel側でStackPanelを保持したいです。
3
+
4
+ MVVMではWindows Forms的な考え方は捨ててください。
5
+ 「Windows Forms的な考え方」とは、
6
+ > コードビハインドでは「spanel.Children.Add(textBlock);」のように書く
7
+
8
+ のようなことです。
9
+ まったく手法が違うので最初は頭の切り替えが大変なのですが、ここがWPFの肝ですし頑張ってください(WPFでもWindows Forms的にコードビハインドでガリガリ書くのを否定するつもりはありません)
10
+
11
+ MVVMについては良記事がたくさんありますので、検索してください^^;
12
+
13
+ ---
14
+
15
+ 今回の目標は「StackPanelに動的にTextBlockを追加」ということですね。
16
+
17
+ 「`StackPanel`」ということは、アイテムが複数あるということです。
18
+ そして「動的」ということは、アイテムの増減があるということです。
19
+
20
+ この時点で、
21
+ * `View`では`ItemsControl`(`ListBox`や`DataGrid`等も含む)を使う
22
+ * `ViewModel`では`ObservableCollection`を使う
23
+
24
+ ことが確定します(もうそういうもんだと覚えてください^^;
25
+
26
+ [ItemsControl 攻略 ~ 外観のカスタマイズ | grabacr.nét](http://grabacr.net/archives/1240)
27
+
28
+ [ObservableCollection<T> クラス (System.Collections.ObjectModel) | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/api/system.collections.objectmodel.observablecollection-1?view=net-6.0)
29
+
30
+ コードを見てもらったほうが早いですが、`ViewModel`では文字列のコレクションとして保持し、`View`でその文字列を`DataTemplate`で`TextBlock.Run`に展開する感じです^^
31
+
32
+
33
+ ```xml
34
+ <Window
35
+ x:Class="Questions376475.MainWindow"
36
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
37
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
38
+ xmlns:local="clr-namespace:Questions376475"
39
+ Width="525"
40
+ Height="350">
41
+ <Window.DataContext>
42
+ <local:ViewModel />
43
+ </Window.DataContext>
44
+ <DockPanel>
45
+ <Label Content="{Binding TestLabel}" DockPanel.Dock="Top" />
46
+
47
+ <Button
48
+ Command="{Binding AddCommand}"
49
+ Content="Add"
50
+ DockPanel.Dock="Bottom" />
51
+
52
+ <ItemsControl ItemsSource="{Binding ItemList}">
53
+ <ItemsControl.ItemTemplate>
54
+ <DataTemplate>
55
+ <TextBlock>
56
+ <Run Foreground="Red" Text="{Binding .}" />
57
+ </TextBlock>
58
+ <!--<TextBlock Foreground="Red" Text="{Binding .}" />-->
59
+ </DataTemplate>
60
+ </ItemsControl.ItemTemplate>
61
+ </ItemsControl>
62
+ </DockPanel>
63
+ </Window>
64
+ ```
65
+
66
+ ```cs
67
+ using System.Collections.ObjectModel;
68
+ using System.Windows;
69
+ using CommunityToolkit.Mvvm.ComponentModel;
70
+ using CommunityToolkit.Mvvm.Input;
71
+
72
+ namespace Questions376475
73
+ {
74
+ public class ViewModel : ObservableObject // ViewModelBase
75
+ {
76
+ private string _testLabel = "初期値";
77
+ public string TestLabel
78
+ {
79
+ get => _testLabel;
80
+ set => SetProperty(ref _testLabel, value);
81
+ }
82
+
83
+ public ObservableCollection<string> ItemList { get; }
84
+
85
+ public RelayCommand AddCommand { get; }
86
+
87
+
88
+ public ViewModel()
89
+ {
90
+ ItemList = new ObservableCollection<string>
91
+ {
92
+ "テスト1",
93
+ "テスト2",
94
+ "テスト3",
95
+ };
96
+
97
+ AddCommand = new RelayCommand(Add);
98
+ }
99
+
100
+ private void Add()
101
+ {
102
+ ItemList.Add($"テスト{ItemList.Count + 1}");
103
+
104
+ TestLabel = $"{ItemList.Count}個です";
105
+ }
106
+ }
107
+
108
+ public partial class MainWindow : Window
109
+ {
110
+ public MainWindow() => InitializeComponent();
111
+ }
112
+ }
113
+ ```
114
+
115
+ `ViewModelBase`や`ICommand`実装を書くのがだるいので、下記を使用しました。
116
+ [NuGet Gallery | CommunityToolkit.Mvvm 7.1.2](https://www.nuget.org/packages/CommunityToolkit.Mvvm/7.1.2)
117
+
118
118
  [Introduction to the MVVM Toolkit - Windows Community Toolkit | Microsoft Docs](https://docs.microsoft.com/ja-jp/windows/communitytoolkit/mvvm/introduction)