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

回答編集履歴

1

見直しキャンペーン中

2023/07/26 15:21

投稿

TN8001
TN8001

スコア10111

answer CHANGED
@@ -1,264 +1,264 @@
1
- > あるコレクションのメソッドから他のコレクションの値を変更する事ってできないですよね?
2
-
3
- `ChangeParameter`のところの話だと思いますが、表示・非表示を引数に全員回せばいいでしょう。
4
- ```C#
5
- ClickMenu = new(x =>
6
- {
7
- foreach (var m in MenuModels)
8
- m.ChangeParameter(x == m);
9
- });
10
- ```
11
-
12
- ---
13
-
14
- とりあえず提示コードを動かすなら、こんな感じでしょうか?
15
- ```xaml
16
- <Window
17
- x:Class="Questions329889.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
18
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:Questions329889" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
19
- Title="MainWindow" Width="23cm" Height="14.2cm"
20
- MinWidth="13cm" MinHeight="14.2cm" mc:Ignorable="d">
21
- <Window.DataContext>
22
- <local:MainViewModel />
23
- </Window.DataContext>
24
- <Window.Resources>
25
- <local:String2BrushConverter x:Key="String2BrushConverter" />
26
- </Window.Resources>
27
- <Grid>
28
- <Grid.ColumnDefinitions>
29
- <ColumnDefinition Width="2cm" />
30
- <ColumnDefinition MinWidth="21" />
31
- </Grid.ColumnDefinitions>
32
-
33
- <Grid Grid.Column="1" Visibility="{Binding MenuModels[0].Visibility}">
34
- <ScrollViewer>
35
- <StackPanel>
36
- <TextBlock
37
- Margin="15,15,0,0" FontSize="23pt" Foreground="#808080"
38
- Text="{Binding MenuModels[0].Content}" />
39
-
40
- <ItemsControl x:Name="MyItems" ItemsSource="{Binding MenuModels}">
41
- <ItemsControl.ItemTemplate>
42
- <DataTemplate>
43
- <Grid Margin="15,15,0,0" Visibility="{Binding HomeMenuVisibility}">
44
- <Border Background="#efebe9" CornerRadius="10" />
45
- <Button
46
- Width="350" Height="160" Background="{x:Null}"
47
- BorderThickness="0" Command="{Binding ElementName=MyItems, Path=DataContext.ClickMenu}" CommandParameter="{Binding}">
48
- <StackPanel
49
- Width="330" Height="140" Margin="10">
50
- <StackPanel Orientation="Horizontal">
51
- <TextBlock
52
- Margin="10,0,10,0" HorizontalAlignment="Center" VerticalAlignment="Center"
53
- FontFamily="Segoe MDL2 Assets" FontSize="18pt" Text="{Binding Icon}" />
54
- <TextBlock FontSize="18pt" Text="{Binding Content}" />
55
- </StackPanel>
56
- <TextBlock
57
- FontSize="11pt" Text="{Binding Note}" TextWrapping="Wrap" />
58
- </StackPanel>
59
- </Button>
60
- </Grid>
61
- </DataTemplate>
62
- </ItemsControl.ItemTemplate>
63
- <ItemsControl.ItemsPanel>
64
- <ItemsPanelTemplate>
65
- <WrapPanel HorizontalAlignment="Center" />
66
- </ItemsPanelTemplate>
67
- </ItemsControl.ItemsPanel>
68
- </ItemsControl>
69
- </StackPanel>
70
- </ScrollViewer>
71
- </Grid>
72
-
73
- <Grid Grid.Column="1" Visibility="{Binding MenuModels[1].Visibility}">
74
- <ScrollViewer>
75
- <StackPanel>
76
- <TextBlock
77
- Margin="15,15,0,0" FontSize="23pt" Foreground="#808080"
78
- Text="{Binding MenuModels[1].Content}" />
79
- </StackPanel>
80
- </ScrollViewer>
81
- </Grid>
82
-
83
- <Grid Grid.Column="1" Visibility="{Binding MenuModels[2].Visibility}">
84
- <ScrollViewer>
85
- <StackPanel>
86
- <TextBlock
87
- Margin="15,15,0,0" FontSize="23pt" Foreground="#808080"
88
- Text="{Binding MenuModels[2].Content}" />
89
- </StackPanel>
90
- </ScrollViewer>
91
- </Grid>
92
-
93
- <Grid Grid.Column="1" Visibility="{Binding MenuModels[3].Visibility}">
94
- <ScrollViewer>
95
- <StackPanel>
96
- <TextBlock
97
- Margin="15,15,0,0" FontSize="23pt" Foreground="#808080"
98
- Text="{Binding MenuModels[3].Content}" />
99
- </StackPanel>
100
- </ScrollViewer>
101
- </Grid>
102
-
103
- <Grid Background="#808080">
104
- <ItemsControl x:Name="MyItemsMenu" ItemsSource="{Binding MenuModels}">
105
- <ItemsControl.ItemTemplate>
106
- <DataTemplate>
107
- <Button
108
- Padding="10" Background="{Binding SideMenuBackground, Converter={StaticResource String2BrushConverter}}" BorderThickness="0"
109
- Command="{Binding DataContext.ClickMenu, ElementName=MyItemsMenu}" CommandParameter="{Binding}" Foreground="{Binding SideMenuForeground, Converter={StaticResource String2BrushConverter}}">
110
- <StackPanel>
111
- <TextBlock
112
- FontFamily="Segoe MDL2 Assets" FontSize="26pt" Text="{Binding Icon}"
113
- TextAlignment="Center" />
114
- <TextBlock
115
- FontSize="8pt" Text="{Binding Content}" TextAlignment="Center" />
116
- </StackPanel>
117
- </Button>
118
- </DataTemplate>
119
- </ItemsControl.ItemTemplate>
120
- <ItemsControl.ItemsPanel>
121
- <ItemsPanelTemplate>
122
- <StackPanel />
123
- </ItemsPanelTemplate>
124
- </ItemsControl.ItemsPanel>
125
- </ItemsControl>
126
- </Grid>
127
- </Grid>
128
- </Window>
129
- ```
130
-
131
- ```C#
132
- using Prism.Commands;
133
- using Prism.Mvvm;
134
- using System;
135
- using System.Collections.ObjectModel;
136
- using System.Diagnostics;
137
- using System.Globalization;
138
- using System.Windows;
139
- using System.Windows.Data;
140
- using System.Windows.Media;
141
-
142
- namespace Questions329889
143
- {
144
- public class String2BrushConverter : IValueConverter
145
- {
146
- public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
147
- {
148
- if (value is string s)
149
- return new SolidColorBrush((Color)ColorConverter.ConvertFromString(s));
150
- return null;
151
- }
152
- public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => throw new NotImplementedException();
153
- }
154
-
155
- public class MenuModel : BindableBase
156
- {
157
- public string SideMenuForeground { get => _SideMenuForeground; set => SetProperty(ref _SideMenuForeground, value); }
158
- private string _SideMenuForeground;
159
-
160
- public string SideMenuBackground { get => _SideMenuBackground; set => SetProperty(ref _SideMenuBackground, value); }
161
- private string _SideMenuBackground;
162
-
163
- public Visibility HomeMenuVisibility { get => _HomeMenuVisibility; set => SetProperty(ref _HomeMenuVisibility, value); }
164
- private Visibility _HomeMenuVisibility;
165
-
166
- public Visibility SideMenuVisibility { get => _SideMenuVisibility; set => SetProperty(ref _SideMenuVisibility, value); }
167
- private Visibility _SideMenuVisibility;
168
-
169
- public Visibility Visibility { get => _Visibility; set => SetProperty(ref _Visibility, value); }
170
- private Visibility _Visibility;
171
-
172
- public string Icon { get => _Icon; set => SetProperty(ref _Icon, value); }
173
- private string _Icon;
174
-
175
- public string Content { get => _Content; set => SetProperty(ref _Content, value); }
176
- private string _Content;
177
-
178
- public string Note { get => _Note; set => SetProperty(ref _Note, value); }
179
- private string _Note;
180
-
181
- public void ChangeParameter(bool visible)
182
- {
183
- if (visible) Debug.WriteLine($"Visible: {Content}");
184
- Visibility = visible ? Visibility.Visible : Visibility.Hidden;
185
- SideMenuBackground = visible ? "#fff" : null;
186
- SideMenuForeground = visible ? "#000" : "#fff";
187
- }
188
- }
189
-
190
- public class MainViewModel : BindableBase
191
- {
192
- public ObservableCollection<MenuModel> MenuModels { get; }
193
- public DelegateCommand<MenuModel> ClickMenu { get; }
194
-
195
- public MainViewModel()
196
- {
197
- MenuModels = new()
198
- {
199
- new()
200
- {
201
- SideMenuBackground = "#fff",
202
- SideMenuForeground = "#000",
203
- HomeMenuVisibility = Visibility.Collapsed,
204
- SideMenuVisibility = Visibility.Visible,
205
- Visibility = Visibility.Visible,
206
- Icon = "\xE10F",
207
- Content = "HOME",
208
- },
209
- new()
210
- {
211
- SideMenuForeground = "#fff",
212
- HomeMenuVisibility = Visibility.Visible,
213
- SideMenuVisibility = Visibility.Visible,
214
- Visibility = Visibility.Hidden,
215
- Icon = "\xF157",
216
- Content = "CONTENT1",
217
- Note = "Content1 です。ここには、content1 の説明が入ります。",
218
- },
219
- new()
220
- {
221
- SideMenuForeground = "#fff",
222
- HomeMenuVisibility = Visibility.Visible,
223
- SideMenuVisibility = Visibility.Visible,
224
- Visibility = Visibility.Hidden,
225
- Icon = "\xF158",
226
- Content = "CONTENT2",
227
- Note = "Content2 です。ここには、content2 の説明が入ります。",
228
- },
229
- new()
230
- {
231
- SideMenuForeground = "#fff",
232
- HomeMenuVisibility = Visibility.Visible,
233
- SideMenuVisibility = Visibility.Visible,
234
- Visibility = Visibility.Hidden,
235
- Icon = "\xF159",
236
- Content = "CONTENT3",
237
- Note = "Content3 です。ここには、content3 の説明が入ります。",
238
- },
239
- };
240
-
241
- ClickMenu = new(x =>
242
- {
243
- foreach (var m in MenuModels)
244
- m.ChangeParameter(x == m);
245
- });
246
- }
247
- }
248
-
249
- public partial class MainWindow : Window
250
- {
251
- public MainWindow() => InitializeComponent();
252
- }
253
- }
254
- ```
255
-
256
- ---
257
-
258
- 気になった点
259
- * ボタンの扱いがおかしい(文字等がある部分までマウスを入れないと反応しない)
260
- 見た目を変えるにはテンプレートを当ててください。
261
- * `View`だけの関心が`ViewModel`(`MenuModel`)に染み出している
262
- `MenuModel`では選択されているという情報だけでいいと思います。
263
- * おそらく`TabControl`ですっきり書けそう
1
+ > あるコレクションのメソッドから他のコレクションの値を変更する事ってできないですよね?
2
+
3
+ `ChangeParameter`のところの話だと思いますが、表示・非表示を引数に全員回せばいいでしょう。
4
+ ```cs
5
+ ClickMenu = new(x =>
6
+ {
7
+ foreach (var m in MenuModels)
8
+ m.ChangeParameter(x == m);
9
+ });
10
+ ```
11
+
12
+ ---
13
+
14
+ とりあえず提示コードを動かすなら、こんな感じでしょうか?
15
+ ```xml
16
+ <Window
17
+ x:Class="Questions329889.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
18
+ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:Questions329889" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
19
+ Title="MainWindow" Width="23cm" Height="14.2cm"
20
+ MinWidth="13cm" MinHeight="14.2cm" mc:Ignorable="d">
21
+ <Window.DataContext>
22
+ <local:MainViewModel />
23
+ </Window.DataContext>
24
+ <Window.Resources>
25
+ <local:String2BrushConverter x:Key="String2BrushConverter" />
26
+ </Window.Resources>
27
+ <Grid>
28
+ <Grid.ColumnDefinitions>
29
+ <ColumnDefinition Width="2cm" />
30
+ <ColumnDefinition MinWidth="21" />
31
+ </Grid.ColumnDefinitions>
32
+
33
+ <Grid Grid.Column="1" Visibility="{Binding MenuModels[0].Visibility}">
34
+ <ScrollViewer>
35
+ <StackPanel>
36
+ <TextBlock
37
+ Margin="15,15,0,0" FontSize="23pt" Foreground="#808080"
38
+ Text="{Binding MenuModels[0].Content}" />
39
+
40
+ <ItemsControl x:Name="MyItems" ItemsSource="{Binding MenuModels}">
41
+ <ItemsControl.ItemTemplate>
42
+ <DataTemplate>
43
+ <Grid Margin="15,15,0,0" Visibility="{Binding HomeMenuVisibility}">
44
+ <Border Background="#efebe9" CornerRadius="10" />
45
+ <Button
46
+ Width="350" Height="160" Background="{x:Null}"
47
+ BorderThickness="0" Command="{Binding ElementName=MyItems, Path=DataContext.ClickMenu}" CommandParameter="{Binding}">
48
+ <StackPanel
49
+ Width="330" Height="140" Margin="10">
50
+ <StackPanel Orientation="Horizontal">
51
+ <TextBlock
52
+ Margin="10,0,10,0" HorizontalAlignment="Center" VerticalAlignment="Center"
53
+ FontFamily="Segoe MDL2 Assets" FontSize="18pt" Text="{Binding Icon}" />
54
+ <TextBlock FontSize="18pt" Text="{Binding Content}" />
55
+ </StackPanel>
56
+ <TextBlock
57
+ FontSize="11pt" Text="{Binding Note}" TextWrapping="Wrap" />
58
+ </StackPanel>
59
+ </Button>
60
+ </Grid>
61
+ </DataTemplate>
62
+ </ItemsControl.ItemTemplate>
63
+ <ItemsControl.ItemsPanel>
64
+ <ItemsPanelTemplate>
65
+ <WrapPanel HorizontalAlignment="Center" />
66
+ </ItemsPanelTemplate>
67
+ </ItemsControl.ItemsPanel>
68
+ </ItemsControl>
69
+ </StackPanel>
70
+ </ScrollViewer>
71
+ </Grid>
72
+
73
+ <Grid Grid.Column="1" Visibility="{Binding MenuModels[1].Visibility}">
74
+ <ScrollViewer>
75
+ <StackPanel>
76
+ <TextBlock
77
+ Margin="15,15,0,0" FontSize="23pt" Foreground="#808080"
78
+ Text="{Binding MenuModels[1].Content}" />
79
+ </StackPanel>
80
+ </ScrollViewer>
81
+ </Grid>
82
+
83
+ <Grid Grid.Column="1" Visibility="{Binding MenuModels[2].Visibility}">
84
+ <ScrollViewer>
85
+ <StackPanel>
86
+ <TextBlock
87
+ Margin="15,15,0,0" FontSize="23pt" Foreground="#808080"
88
+ Text="{Binding MenuModels[2].Content}" />
89
+ </StackPanel>
90
+ </ScrollViewer>
91
+ </Grid>
92
+
93
+ <Grid Grid.Column="1" Visibility="{Binding MenuModels[3].Visibility}">
94
+ <ScrollViewer>
95
+ <StackPanel>
96
+ <TextBlock
97
+ Margin="15,15,0,0" FontSize="23pt" Foreground="#808080"
98
+ Text="{Binding MenuModels[3].Content}" />
99
+ </StackPanel>
100
+ </ScrollViewer>
101
+ </Grid>
102
+
103
+ <Grid Background="#808080">
104
+ <ItemsControl x:Name="MyItemsMenu" ItemsSource="{Binding MenuModels}">
105
+ <ItemsControl.ItemTemplate>
106
+ <DataTemplate>
107
+ <Button
108
+ Padding="10" Background="{Binding SideMenuBackground, Converter={StaticResource String2BrushConverter}}" BorderThickness="0"
109
+ Command="{Binding DataContext.ClickMenu, ElementName=MyItemsMenu}" CommandParameter="{Binding}" Foreground="{Binding SideMenuForeground, Converter={StaticResource String2BrushConverter}}">
110
+ <StackPanel>
111
+ <TextBlock
112
+ FontFamily="Segoe MDL2 Assets" FontSize="26pt" Text="{Binding Icon}"
113
+ TextAlignment="Center" />
114
+ <TextBlock
115
+ FontSize="8pt" Text="{Binding Content}" TextAlignment="Center" />
116
+ </StackPanel>
117
+ </Button>
118
+ </DataTemplate>
119
+ </ItemsControl.ItemTemplate>
120
+ <ItemsControl.ItemsPanel>
121
+ <ItemsPanelTemplate>
122
+ <StackPanel />
123
+ </ItemsPanelTemplate>
124
+ </ItemsControl.ItemsPanel>
125
+ </ItemsControl>
126
+ </Grid>
127
+ </Grid>
128
+ </Window>
129
+ ```
130
+
131
+ ```cs
132
+ using Prism.Commands;
133
+ using Prism.Mvvm;
134
+ using System;
135
+ using System.Collections.ObjectModel;
136
+ using System.Diagnostics;
137
+ using System.Globalization;
138
+ using System.Windows;
139
+ using System.Windows.Data;
140
+ using System.Windows.Media;
141
+
142
+ namespace Questions329889
143
+ {
144
+ public class String2BrushConverter : IValueConverter
145
+ {
146
+ public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
147
+ {
148
+ if (value is string s)
149
+ return new SolidColorBrush((Color)ColorConverter.ConvertFromString(s));
150
+ return null;
151
+ }
152
+ public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => throw new NotImplementedException();
153
+ }
154
+
155
+ public class MenuModel : BindableBase
156
+ {
157
+ public string SideMenuForeground { get => _SideMenuForeground; set => SetProperty(ref _SideMenuForeground, value); }
158
+ private string _SideMenuForeground;
159
+
160
+ public string SideMenuBackground { get => _SideMenuBackground; set => SetProperty(ref _SideMenuBackground, value); }
161
+ private string _SideMenuBackground;
162
+
163
+ public Visibility HomeMenuVisibility { get => _HomeMenuVisibility; set => SetProperty(ref _HomeMenuVisibility, value); }
164
+ private Visibility _HomeMenuVisibility;
165
+
166
+ public Visibility SideMenuVisibility { get => _SideMenuVisibility; set => SetProperty(ref _SideMenuVisibility, value); }
167
+ private Visibility _SideMenuVisibility;
168
+
169
+ public Visibility Visibility { get => _Visibility; set => SetProperty(ref _Visibility, value); }
170
+ private Visibility _Visibility;
171
+
172
+ public string Icon { get => _Icon; set => SetProperty(ref _Icon, value); }
173
+ private string _Icon;
174
+
175
+ public string Content { get => _Content; set => SetProperty(ref _Content, value); }
176
+ private string _Content;
177
+
178
+ public string Note { get => _Note; set => SetProperty(ref _Note, value); }
179
+ private string _Note;
180
+
181
+ public void ChangeParameter(bool visible)
182
+ {
183
+ if (visible) Debug.WriteLine($"Visible: {Content}");
184
+ Visibility = visible ? Visibility.Visible : Visibility.Hidden;
185
+ SideMenuBackground = visible ? "#fff" : null;
186
+ SideMenuForeground = visible ? "#000" : "#fff";
187
+ }
188
+ }
189
+
190
+ public class MainViewModel : BindableBase
191
+ {
192
+ public ObservableCollection<MenuModel> MenuModels { get; }
193
+ public DelegateCommand<MenuModel> ClickMenu { get; }
194
+
195
+ public MainViewModel()
196
+ {
197
+ MenuModels = new()
198
+ {
199
+ new()
200
+ {
201
+ SideMenuBackground = "#fff",
202
+ SideMenuForeground = "#000",
203
+ HomeMenuVisibility = Visibility.Collapsed,
204
+ SideMenuVisibility = Visibility.Visible,
205
+ Visibility = Visibility.Visible,
206
+ Icon = "\xE10F",
207
+ Content = "HOME",
208
+ },
209
+ new()
210
+ {
211
+ SideMenuForeground = "#fff",
212
+ HomeMenuVisibility = Visibility.Visible,
213
+ SideMenuVisibility = Visibility.Visible,
214
+ Visibility = Visibility.Hidden,
215
+ Icon = "\xF157",
216
+ Content = "CONTENT1",
217
+ Note = "Content1 です。ここには、content1 の説明が入ります。",
218
+ },
219
+ new()
220
+ {
221
+ SideMenuForeground = "#fff",
222
+ HomeMenuVisibility = Visibility.Visible,
223
+ SideMenuVisibility = Visibility.Visible,
224
+ Visibility = Visibility.Hidden,
225
+ Icon = "\xF158",
226
+ Content = "CONTENT2",
227
+ Note = "Content2 です。ここには、content2 の説明が入ります。",
228
+ },
229
+ new()
230
+ {
231
+ SideMenuForeground = "#fff",
232
+ HomeMenuVisibility = Visibility.Visible,
233
+ SideMenuVisibility = Visibility.Visible,
234
+ Visibility = Visibility.Hidden,
235
+ Icon = "\xF159",
236
+ Content = "CONTENT3",
237
+ Note = "Content3 です。ここには、content3 の説明が入ります。",
238
+ },
239
+ };
240
+
241
+ ClickMenu = new(x =>
242
+ {
243
+ foreach (var m in MenuModels)
244
+ m.ChangeParameter(x == m);
245
+ });
246
+ }
247
+ }
248
+
249
+ public partial class MainWindow : Window
250
+ {
251
+ public MainWindow() => InitializeComponent();
252
+ }
253
+ }
254
+ ```
255
+
256
+ ---
257
+
258
+ 気になった点
259
+ * ボタンの扱いがおかしい(文字等がある部分までマウスを入れないと反応しない)
260
+ 見た目を変えるにはテンプレートを当ててください。
261
+ * `View`だけの関心が`ViewModel`(`MenuModel`)に染み出している
262
+ `MenuModel`では選択されているという情報だけでいいと思います。
263
+ * おそらく`TabControl`ですっきり書けそう
264
264
  見た目を同じにするとなると、スタイル等で行数は変わらないかも?