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

回答編集履歴

2

見直しキャンペーン中

2023/07/26 16:10

投稿

TN8001
TN8001

スコア10114

answer CHANGED
@@ -1,233 +1,233 @@
1
- はっきり言って丸投げ感が半端ないですが、どんどんおかしな方向に行っているので回答します(前回回答分ぐらいは反映して頂きたかったのですが。。)
2
-
3
- 前回の質問では何をしようとしているのかが見えなかったのでスルーしましたが、`DependencyProperty`などは全く必要ありません。
4
- これに関してはPrismが悪いと思っているので、K.KATSU2さんのせいではありません。
5
-
6
- ちょっと日本語でうまく説明できないのでコードを見てください。
7
-
8
- MainWindow
9
- ```xaml
10
- <Window
11
- x:Class="Questions334328.Views.MainWindow"
12
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
13
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
14
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
15
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
16
- xmlns:prism="http://prismlibrary.com/"
17
- xmlns:viewmodels="clr-namespace:Questions334328.ViewModels"
18
- xmlns:views="clr-namespace:Questions334328.Views"
19
- Width="500"
20
- Height="360"
21
- d:DataContext="{d:DesignInstance Type=viewmodels:MainWindowViewModel}"
22
- prism:ViewModelLocator.AutoWireViewModel="True"
23
- mc:Ignorable="d">
24
-
25
- <Window.Resources>
26
- <Style TargetType="Button">
27
- <Setter Property="Margin" Value="10" />
28
- <Style.Triggers>
29
- <Trigger Property="Command" Value="{x:Null}">
30
- <Setter Property="IsEnabled" Value="False" />
31
- </Trigger>
32
- </Style.Triggers>
33
- </Style>
34
- <DataTemplate DataType="{x:Type viewmodels:ActionPanelViewModel}">
35
- <views:ActionPanel />
36
- </DataTemplate>
37
- </Window.Resources>
38
-
39
- <DockPanel>
40
- <StackPanel MinWidth="100" DockPanel.Dock="Right">
41
- <Button Command="{Binding AddCommand}" Content="追加" />
42
- <Button
43
- Command="{Binding DelCommand}"
44
- CommandParameter="{Binding Items/}"
45
- Content="削除" />
46
- <Button Command="{Binding Items/OrCommand}" Content="Or" />
47
- <Button Command="{Binding Items/AndCommand}" Content="And" />
48
- </StackPanel>
49
-
50
- <DockPanel>
51
- <DockPanel DockPanel.Dock="Top">
52
- <Label Content="承認フロー名:" />
53
- <TextBox />
54
- </DockPanel>
55
- <!-- IsSynchronizedWithCurrentItem="True"がないと / が利かないので注意 -->
56
- <ListBox
57
- HorizontalContentAlignment="Stretch"
58
- IsSynchronizedWithCurrentItem="True"
59
- ItemsSource="{Binding Items}" />
60
- </DockPanel>
61
- </DockPanel>
62
- </Window>
63
- ```
64
- ```C#
65
- using System.Windows;
66
-
67
- namespace Questions334328.Views
68
- {
69
- public partial class MainWindow : Window
70
- {
71
- public MainWindow()
72
- {
73
- InitializeComponent();
74
- }
75
- }
76
- }
77
- ```
78
- ```C#
79
- using Prism.Commands;
80
- using Prism.Mvvm;
81
- using System.Collections.ObjectModel;
82
-
83
- namespace Questions334328.ViewModels
84
- {
85
- public class MainWindowViewModel : BindableBase
86
- {
87
- public ObservableCollection<ActionPanelViewModel> Items { get; } = new ObservableCollection<ActionPanelViewModel>();
88
-
89
- public DelegateCommand AddCommand { get; }
90
- public DelegateCommand<ActionPanelViewModel> DelCommand { get; }
91
-
92
- public MainWindowViewModel()
93
- {
94
- AddCommand = new DelegateCommand(ExecuteAddCommand);
95
- DelCommand = new DelegateCommand<ActionPanelViewModel>(ExecuteDelCommand, CanExecuteDelCommand)
96
- .ObservesProperty(() => Items.Count); // アイテムがなければ押せない どのプロパティを監視してCanExecuteDelCommandを走らせるかをラムダ式(式木)で指定
97
- }
98
-
99
- private void ExecuteAddCommand()
100
- {
101
- Items.Add(new ActionPanelViewModel { No = Items.Count + 1, });
102
- }
103
-
104
- private bool CanExecuteDelCommand(ActionPanelViewModel arg)
105
- {
106
- return 0 < Items.Count;
107
- }
108
- private void ExecuteDelCommand(ActionPanelViewModel item)
109
- {
110
- Items.Remove(item);
111
- for (var i = 0; i < Items.Count; i++) Items[i].No = i + 1; // No振り直し
112
- }
113
- }
114
- }
115
- ```
116
-
117
- ActionPanel
118
- ```xaml
119
- <UserControl
120
- x:Class="Questions334328.Views.ActionPanel"
121
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
122
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
123
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
124
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
125
- xmlns:viewmodels="clr-namespace:Questions334328.ViewModels"
126
- d:DataContext="{d:DesignInstance Type=viewmodels:ActionPanelViewModel}"
127
- d:DesignHeight="60"
128
- d:DesignWidth="340"
129
- mc:Ignorable="d">
130
- <StackPanel>
131
- <Grid>
132
- <Grid.ColumnDefinitions>
133
- <ColumnDefinition Width="Auto" MinWidth="20" />
134
- <ColumnDefinition Width="173*" />
135
- <ColumnDefinition Width="130*" />
136
- </Grid.ColumnDefinitions>
137
- <TextBlock FontSize="14" Text="{Binding No}" />
138
- <ComboBox
139
- Grid.Column="1"
140
- Margin="10,0"
141
- DisplayMemberPath="DisplayValue"
142
- ItemsSource="{Binding People}"
143
- SelectedItem="{Binding SelectedAuthority, Mode=TwoWay}"
144
- SelectedValue="Value" />
145
- <ComboBox
146
- Grid.Column="2"
147
- Margin="10,0"
148
- DisplayMemberPath="DisplayValue"
149
- ItemsSource="{Binding Authority}"
150
- SelectedItem="{Binding SelectedAuthority, Mode=TwoWay}"
151
- SelectedValue="Value" />
152
- </Grid>
153
-
154
- <Label
155
- Content="{Binding Indicator}"
156
- FontFamily="Yu Gothic UI Semibold"
157
- FontSize="14"
158
- FontWeight="Bold" />
159
- </StackPanel>
160
- </UserControl>
161
- ```
162
- ```C#
163
- using System.Windows.Controls;
164
-
165
- namespace Questions334328.Views
166
- {
167
- public partial class ActionPanel : UserControl
168
- {
169
- public ActionPanel()
170
- {
171
- InitializeComponent();
172
- }
173
- }
174
- }
175
- ```
176
- ```C#
177
- using Prism.Commands;
178
- using Prism.Mvvm;
179
- using System;
180
-
181
- namespace Questions334328.ViewModels
182
- {
183
- public class ActionPanelViewModel : BindableBase
184
- {
185
- private int _No;
186
- public int No { get => _No; set => SetProperty(ref _No, value); }
187
-
188
- private string _Indicator = "↓";
189
- public string Indicator { get => _Indicator; set => SetProperty(ref _Indicator, value); }
190
-
191
- public DelegateCommand OrCommand { get; }
192
- public DelegateCommand AndCommand { get; }
193
-
194
- public ActionPanelViewModel()
195
- {
196
- OrCommand = new DelegateCommand(ExecuteOrCommand/*() => Indicator = "Or"*/);
197
- AndCommand = new DelegateCommand(ExecuteAndCommand/*() => Indicator = "And"*/);
198
- }
199
-
200
- private void ExecuteOrCommand()
201
- {
202
- Indicator = "Or";
203
- }
204
- private void ExecuteAndCommand()
205
- {
206
- Indicator = "And";
207
- }
208
- }
209
- }
210
- ```
211
-
212
- 質問コードでは`Item`が`DataTemplate`で`ActionPanel`になり、`DataContext`に`ActionPanelViewModel`がインジェクションされるという流れです。
213
- ロジック上は不要な`Item`があったり、`ViewModel`がインジェクションされるため「どういじればいいんだ?」となります。
214
-
215
- 回答コードは`ActionPanelViewModel`が`DataTemplate`で`ActionPanel`になり、`DataContext`は`ActionPanelViewModel`をそのまま引き継ぎます。
216
- `ActionPanelViewModel`の増減やプロパティの変更が、そのまま`View`に反映されます。
217
-
218
- 言葉以上にシンプルで非常にすっきりしていると感じられると思います^^
219
-
220
-
221
- `OrCommand`・`AndCommand`は`ActionPanelViewModel`側で実装しましたが、メイン側に移したければ`DelCommand`と同じようにするだけです。
222
-
223
- `{Binding Items/}`等の`/`については↓を参照してください。
224
- [データ バインディングの概要 - WPF .NET Framework | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/desktop/wpf/data/data-binding-overview?view=netframeworkdesktop-4.8#current-item-pointers)
225
-
226
- [方法: 階層データでマスター詳細パターンを使用する - WPF .NET Framework | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/desktop/wpf/data/how-to-use-the-master-detail-pattern-with-hierarchical-data?view=netframeworkdesktop-4.8&viewFallbackFrom=netdesktop-5.0)
227
-
228
- ---
229
-
230
- 過去にも何度かPrism関連で回答しているのですが、おかしなことになっていることが非常に多いです。
231
- Prismは`UserControl`・`ViewModel`をセットで生成するため、こういった勘違いを誘発している気がします。
232
-
1
+ はっきり言って丸投げ感が半端ないですが、どんどんおかしな方向に行っているので回答します(前回回答分ぐらいは反映して頂きたかったのですが。。)
2
+
3
+ 前回の質問では何をしようとしているのかが見えなかったのでスルーしましたが、`DependencyProperty`などは全く必要ありません。
4
+ これに関してはPrismが悪いと思っているので、K.KATSU2さんのせいではありません。
5
+
6
+ ちょっと日本語でうまく説明できないのでコードを見てください。
7
+
8
+ MainWindow
9
+ ```xml
10
+ <Window
11
+ x:Class="Questions334328.Views.MainWindow"
12
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
13
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
14
+ xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
15
+ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
16
+ xmlns:prism="http://prismlibrary.com/"
17
+ xmlns:viewmodels="clr-namespace:Questions334328.ViewModels"
18
+ xmlns:views="clr-namespace:Questions334328.Views"
19
+ Width="500"
20
+ Height="360"
21
+ d:DataContext="{d:DesignInstance Type=viewmodels:MainWindowViewModel}"
22
+ prism:ViewModelLocator.AutoWireViewModel="True"
23
+ mc:Ignorable="d">
24
+
25
+ <Window.Resources>
26
+ <Style TargetType="Button">
27
+ <Setter Property="Margin" Value="10" />
28
+ <Style.Triggers>
29
+ <Trigger Property="Command" Value="{x:Null}">
30
+ <Setter Property="IsEnabled" Value="False" />
31
+ </Trigger>
32
+ </Style.Triggers>
33
+ </Style>
34
+ <DataTemplate DataType="{x:Type viewmodels:ActionPanelViewModel}">
35
+ <views:ActionPanel />
36
+ </DataTemplate>
37
+ </Window.Resources>
38
+
39
+ <DockPanel>
40
+ <StackPanel MinWidth="100" DockPanel.Dock="Right">
41
+ <Button Command="{Binding AddCommand}" Content="追加" />
42
+ <Button
43
+ Command="{Binding DelCommand}"
44
+ CommandParameter="{Binding Items/}"
45
+ Content="削除" />
46
+ <Button Command="{Binding Items/OrCommand}" Content="Or" />
47
+ <Button Command="{Binding Items/AndCommand}" Content="And" />
48
+ </StackPanel>
49
+
50
+ <DockPanel>
51
+ <DockPanel DockPanel.Dock="Top">
52
+ <Label Content="承認フロー名:" />
53
+ <TextBox />
54
+ </DockPanel>
55
+ <!-- IsSynchronizedWithCurrentItem="True"がないと / が利かないので注意 -->
56
+ <ListBox
57
+ HorizontalContentAlignment="Stretch"
58
+ IsSynchronizedWithCurrentItem="True"
59
+ ItemsSource="{Binding Items}" />
60
+ </DockPanel>
61
+ </DockPanel>
62
+ </Window>
63
+ ```
64
+ ```cs
65
+ using System.Windows;
66
+
67
+ namespace Questions334328.Views
68
+ {
69
+ public partial class MainWindow : Window
70
+ {
71
+ public MainWindow()
72
+ {
73
+ InitializeComponent();
74
+ }
75
+ }
76
+ }
77
+ ```
78
+ ```cs
79
+ using Prism.Commands;
80
+ using Prism.Mvvm;
81
+ using System.Collections.ObjectModel;
82
+
83
+ namespace Questions334328.ViewModels
84
+ {
85
+ public class MainWindowViewModel : BindableBase
86
+ {
87
+ public ObservableCollection<ActionPanelViewModel> Items { get; } = new ObservableCollection<ActionPanelViewModel>();
88
+
89
+ public DelegateCommand AddCommand { get; }
90
+ public DelegateCommand<ActionPanelViewModel> DelCommand { get; }
91
+
92
+ public MainWindowViewModel()
93
+ {
94
+ AddCommand = new DelegateCommand(ExecuteAddCommand);
95
+ DelCommand = new DelegateCommand<ActionPanelViewModel>(ExecuteDelCommand, CanExecuteDelCommand)
96
+ .ObservesProperty(() => Items.Count); // アイテムがなければ押せない どのプロパティを監視してCanExecuteDelCommandを走らせるかをラムダ式(式木)で指定
97
+ }
98
+
99
+ private void ExecuteAddCommand()
100
+ {
101
+ Items.Add(new ActionPanelViewModel { No = Items.Count + 1, });
102
+ }
103
+
104
+ private bool CanExecuteDelCommand(ActionPanelViewModel arg)
105
+ {
106
+ return 0 < Items.Count;
107
+ }
108
+ private void ExecuteDelCommand(ActionPanelViewModel item)
109
+ {
110
+ Items.Remove(item);
111
+ for (var i = 0; i < Items.Count; i++) Items[i].No = i + 1; // No振り直し
112
+ }
113
+ }
114
+ }
115
+ ```
116
+
117
+ ActionPanel
118
+ ```xml
119
+ <UserControl
120
+ x:Class="Questions334328.Views.ActionPanel"
121
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
122
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
123
+ xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
124
+ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
125
+ xmlns:viewmodels="clr-namespace:Questions334328.ViewModels"
126
+ d:DataContext="{d:DesignInstance Type=viewmodels:ActionPanelViewModel}"
127
+ d:DesignHeight="60"
128
+ d:DesignWidth="340"
129
+ mc:Ignorable="d">
130
+ <StackPanel>
131
+ <Grid>
132
+ <Grid.ColumnDefinitions>
133
+ <ColumnDefinition Width="Auto" MinWidth="20" />
134
+ <ColumnDefinition Width="173*" />
135
+ <ColumnDefinition Width="130*" />
136
+ </Grid.ColumnDefinitions>
137
+ <TextBlock FontSize="14" Text="{Binding No}" />
138
+ <ComboBox
139
+ Grid.Column="1"
140
+ Margin="10,0"
141
+ DisplayMemberPath="DisplayValue"
142
+ ItemsSource="{Binding People}"
143
+ SelectedItem="{Binding SelectedAuthority, Mode=TwoWay}"
144
+ SelectedValue="Value" />
145
+ <ComboBox
146
+ Grid.Column="2"
147
+ Margin="10,0"
148
+ DisplayMemberPath="DisplayValue"
149
+ ItemsSource="{Binding Authority}"
150
+ SelectedItem="{Binding SelectedAuthority, Mode=TwoWay}"
151
+ SelectedValue="Value" />
152
+ </Grid>
153
+
154
+ <Label
155
+ Content="{Binding Indicator}"
156
+ FontFamily="Yu Gothic UI Semibold"
157
+ FontSize="14"
158
+ FontWeight="Bold" />
159
+ </StackPanel>
160
+ </UserControl>
161
+ ```
162
+ ```cs
163
+ using System.Windows.Controls;
164
+
165
+ namespace Questions334328.Views
166
+ {
167
+ public partial class ActionPanel : UserControl
168
+ {
169
+ public ActionPanel()
170
+ {
171
+ InitializeComponent();
172
+ }
173
+ }
174
+ }
175
+ ```
176
+ ```cs
177
+ using Prism.Commands;
178
+ using Prism.Mvvm;
179
+ using System;
180
+
181
+ namespace Questions334328.ViewModels
182
+ {
183
+ public class ActionPanelViewModel : BindableBase
184
+ {
185
+ private int _No;
186
+ public int No { get => _No; set => SetProperty(ref _No, value); }
187
+
188
+ private string _Indicator = "↓";
189
+ public string Indicator { get => _Indicator; set => SetProperty(ref _Indicator, value); }
190
+
191
+ public DelegateCommand OrCommand { get; }
192
+ public DelegateCommand AndCommand { get; }
193
+
194
+ public ActionPanelViewModel()
195
+ {
196
+ OrCommand = new DelegateCommand(ExecuteOrCommand/*() => Indicator = "Or"*/);
197
+ AndCommand = new DelegateCommand(ExecuteAndCommand/*() => Indicator = "And"*/);
198
+ }
199
+
200
+ private void ExecuteOrCommand()
201
+ {
202
+ Indicator = "Or";
203
+ }
204
+ private void ExecuteAndCommand()
205
+ {
206
+ Indicator = "And";
207
+ }
208
+ }
209
+ }
210
+ ```
211
+
212
+ 質問コードでは`Item`が`DataTemplate`で`ActionPanel`になり、`DataContext`に`ActionPanelViewModel`がインジェクションされるという流れです。
213
+ ロジック上は不要な`Item`があったり、`ViewModel`がインジェクションされるため「どういじればいいんだ?」となります。
214
+
215
+ 回答コードは`ActionPanelViewModel`が`DataTemplate`で`ActionPanel`になり、`DataContext`は`ActionPanelViewModel`をそのまま引き継ぎます。
216
+ `ActionPanelViewModel`の増減やプロパティの変更が、そのまま`View`に反映されます。
217
+
218
+ 言葉以上にシンプルで非常にすっきりしていると感じられると思います^^
219
+
220
+
221
+ `OrCommand`・`AndCommand`は`ActionPanelViewModel`側で実装しましたが、メイン側に移したければ`DelCommand`と同じようにするだけです。
222
+
223
+ `{Binding Items/}`等の`/`については↓を参照してください。
224
+ [データ バインディングの概要 - WPF .NET Framework | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/desktop/wpf/data/data-binding-overview?view=netframeworkdesktop-4.8#current-item-pointers)
225
+
226
+ [方法: 階層データでマスター詳細パターンを使用する - WPF .NET Framework | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/desktop/wpf/data/how-to-use-the-master-detail-pattern-with-hierarchical-data?view=netframeworkdesktop-4.8&viewFallbackFrom=netdesktop-5.0)
227
+
228
+ ---
229
+
230
+ 過去にも何度かPrism関連で回答しているのですが、おかしなことになっていることが非常に多いです。
231
+ Prismは`UserControl`・`ViewModel`をセットで生成するため、こういった勘違いを誘発している気がします。
232
+
233
233
  WPFに精通した方が大規模開発する場合は有用なんだろうとは思いますが、私はどうも好きになれません^^;

1

できるだけメソッドに書き換え

2021/04/22 05:00

投稿

TN8001
TN8001

スコア10114

answer CHANGED
@@ -68,7 +68,10 @@
68
68
  {
69
69
  public partial class MainWindow : Window
70
70
  {
71
+ public MainWindow()
72
+ {
71
- public MainWindow() => InitializeComponent();
73
+ InitializeComponent();
74
+ }
72
75
  }
73
76
  }
74
77
  ```
@@ -89,15 +92,23 @@
89
92
  public MainWindowViewModel()
90
93
  {
91
94
  AddCommand = new DelegateCommand(ExecuteAddCommand);
92
- DelCommand = new DelegateCommand<ActionPanelViewModel>(ExecuteDelCommand, _ => 0 < Items.Count)
95
+ DelCommand = new DelegateCommand<ActionPanelViewModel>(ExecuteDelCommand, CanExecuteDelCommand)
93
- .ObservesProperty(() => Items.Count); // アイテムがなければ押せない
96
+ .ObservesProperty(() => Items.Count); // アイテムがなければ押せない どのプロパティを監視してCanExecuteDelCommandを走らせるかをラムダ式(式木)で指定
94
97
  }
95
98
 
99
+ private void ExecuteAddCommand()
100
+ {
96
- private void ExecuteAddCommand() => Items.Add(new ActionPanelViewModel { No = Items.Count + 1, });
101
+ Items.Add(new ActionPanelViewModel { No = Items.Count + 1, });
102
+ }
103
+
104
+ private bool CanExecuteDelCommand(ActionPanelViewModel arg)
105
+ {
106
+ return 0 < Items.Count;
107
+ }
97
108
  private void ExecuteDelCommand(ActionPanelViewModel item)
98
109
  {
99
110
  Items.Remove(item);
100
- for (var i = 0; i < Items.Count; i++) Items[i].No = i + 1; // No.振り直し
111
+ for (var i = 0; i < Items.Count; i++) Items[i].No = i + 1; // No振り直し
101
112
  }
102
113
  }
103
114
  }
@@ -155,13 +166,17 @@
155
166
  {
156
167
  public partial class ActionPanel : UserControl
157
168
  {
169
+ public ActionPanel()
170
+ {
158
- public ActionPanel() => InitializeComponent();
171
+ InitializeComponent();
172
+ }
159
173
  }
160
174
  }
161
175
  ```
162
176
  ```C#
163
177
  using Prism.Commands;
164
178
  using Prism.Mvvm;
179
+ using System;
165
180
 
166
181
  namespace Questions334328.ViewModels
167
182
  {
@@ -178,9 +193,18 @@
178
193
 
179
194
  public ActionPanelViewModel()
180
195
  {
181
- OrCommand = new DelegateCommand(() => Indicator = "Or");
196
+ OrCommand = new DelegateCommand(ExecuteOrCommand/*() => Indicator = "Or"*/);
182
- AndCommand = new DelegateCommand(() => Indicator = "And");
197
+ AndCommand = new DelegateCommand(ExecuteAndCommand/*() => Indicator = "And"*/);
183
198
  }
199
+
200
+ private void ExecuteOrCommand()
201
+ {
202
+ Indicator = "Or";
203
+ }
204
+ private void ExecuteAndCommand()
205
+ {
206
+ Indicator = "And";
207
+ }
184
208
  }
185
209
  }
186
210
  ```