回答編集履歴

1

見直しキャンペーン中

2023/07/21 09:04

投稿

TN8001
TN8001

スコア9862

test CHANGED
@@ -1,311 +1,156 @@
1
1
  > これを特定のセルを指定することは可能でしょうか?
2
-
3
-
4
2
 
5
3
  `GridViewColumn.CellTemplate`で変更することになります。
6
4
 
7
-
8
-
9
5
  雑で申し訳ないですが、色の変更までの例です。
10
-
11
- ```xaml
6
+ ```xml
12
-
13
7
  <Window
14
-
15
8
  x:Class="Questions248072.MainWindow"
16
-
17
9
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
18
-
19
10
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
20
-
21
11
  Title="MainWindow"
22
-
23
12
  Width="800"
24
-
25
13
  Height="450">
26
-
27
14
  <Grid>
28
-
29
15
  <ListView ItemsSource="{Binding Users}">
30
-
31
16
  <ListView.Resources>
32
17
 
18
+ <ContextMenu x:Key="ContextMenu">
19
+ <MenuItem
20
+ Click="MenuItem_Click"
21
+ Header="通常"
22
+ IsChecked="{Binding IsWhite, Mode=OneWay}" />
23
+ <MenuItem
24
+ Click="MenuItem_Click"
25
+ Header="黄色"
26
+ IsChecked="{Binding IsYellow, Mode=OneWay}" />
27
+ <MenuItem
28
+ Click="MenuItem_Click"
29
+ Header="赤"
30
+ IsChecked="{Binding IsRed, Mode=OneWay}" />
31
+ </ContextMenu>
32
+ </ListView.Resources>
33
+
34
+ <ListView.ItemContainerStyle>
35
+ <Style TargetType="{x:Type ListViewItem}">
36
+ <Setter Property="HorizontalContentAlignment" Value="Stretch" />
37
+ <Setter Property="ContextMenu" Value="{StaticResource ContextMenu}" />
38
+ </Style>
39
+ </ListView.ItemContainerStyle>
40
+
41
+ <ListView.View>
42
+ <GridView>
43
+ <GridViewColumn
44
+ Width="170"
45
+ DisplayMemberBinding="{Binding No}"
46
+ Header="No" />
33
47
 
34
-
35
- <ContextMenu x:Key="ContextMenu">
48
+ <GridViewColumn Width="80" Header="Name">
36
-
37
- <MenuItem
38
-
39
- Click="MenuItem_Click"
40
-
41
- Header="通常"
42
-
43
- IsChecked="{Binding IsWhite, Mode=OneWay}" />
44
-
45
- <MenuItem
46
-
47
- Click="MenuItem_Click"
48
-
49
- Header="黄色"
50
-
51
- IsChecked="{Binding IsYellow, Mode=OneWay}" />
52
-
53
- <MenuItem
54
-
55
- Click="MenuItem_Click"
56
-
57
- Header="赤"
58
-
59
- IsChecked="{Binding IsRed, Mode=OneWay}" />
60
-
61
- </ContextMenu>
62
-
63
- </ListView.Resources>
64
-
65
-
66
-
67
- <ListView.ItemContainerStyle>
49
+ <GridViewColumn.CellTemplate>
68
-
50
+ <DataTemplate>
69
- <Style TargetType="{x:Type ListViewItem}">
51
+ <Border x:Name="bg" Margin="-6,0,-6,0">
70
-
71
- <Setter Property="HorizontalContentAlignment" Value="Stretch" />
72
-
73
- <Setter Property="ContextMenu" Value="{StaticResource ContextMenu}" />
52
+ <TextBlock Margin="6,0,6,0" Text="{Binding Name}" />
74
-
75
- </Style>
53
+ </Border>
76
-
54
+ <DataTemplate.Triggers>
55
+ <DataTrigger Binding="{Binding BkColor}" Value="Red">
56
+ <Setter TargetName="bg" Property="Background" Value="Red" />
57
+ </DataTrigger>
58
+ <DataTrigger Binding="{Binding BkColor}" Value="Yellow">
59
+ <Setter TargetName="bg" Property="Background" Value="#FE0" />
60
+ </DataTrigger>
61
+ </DataTemplate.Triggers>
62
+ </DataTemplate>
77
- </ListView.ItemContainerStyle>
63
+ </GridViewColumn.CellTemplate>
78
-
79
-
80
-
81
- <ListView.View>
82
-
83
- <GridView>
64
+ </GridViewColumn>
84
65
 
85
66
  <GridViewColumn
86
-
87
- Width="170"
88
-
89
- DisplayMemberBinding="{Binding No}"
90
-
91
- Header="No" />
92
-
93
-
94
-
95
- <GridViewColumn Width="80" Header="Name">
96
-
97
- <GridViewColumn.CellTemplate>
98
-
99
- <DataTemplate>
100
-
101
- <Border x:Name="bg" Margin="-6,0,-6,0">
102
-
103
- <TextBlock Margin="6,0,6,0" Text="{Binding Name}" />
104
-
105
- </Border>
106
-
107
- <DataTemplate.Triggers>
108
-
109
- <DataTrigger Binding="{Binding BkColor}" Value="Red">
110
-
111
- <Setter TargetName="bg" Property="Background" Value="Red" />
112
-
113
- </DataTrigger>
114
-
115
- <DataTrigger Binding="{Binding BkColor}" Value="Yellow">
116
-
117
- <Setter TargetName="bg" Property="Background" Value="#FE0" />
118
-
119
- </DataTrigger>
120
-
121
- </DataTemplate.Triggers>
122
-
123
- </DataTemplate>
124
-
125
- </GridViewColumn.CellTemplate>
126
-
127
- </GridViewColumn>
128
-
129
-
130
-
131
- <GridViewColumn
132
-
133
67
  Width="80"
134
-
135
68
  DisplayMemberBinding="{Binding Data}"
136
-
137
69
  Header="Data" />
138
-
139
70
  </GridView>
140
-
141
71
  </ListView.View>
142
-
143
72
  </ListView>
144
-
145
73
  </Grid>
146
-
147
74
  </Window>
148
-
149
75
  ```
150
76
 
151
-
152
-
153
- ```C#
77
+ ```cs
154
-
155
78
  using System.Collections.ObjectModel;
156
-
157
79
  using System.ComponentModel;
158
-
159
80
  using System.Runtime.CompilerServices;
160
-
161
81
  using System.Windows;
162
-
163
82
  using System.Windows.Controls;
164
83
 
165
-
166
-
167
84
  namespace Questions248072
168
-
169
85
  {
170
-
171
86
  public class User : INotifyPropertyChanged
172
-
173
87
  {
174
-
175
88
  public string No { get; set; }
176
-
177
89
  public string Name { get; set; }
178
-
179
90
  public string Data { get; set; }
180
-
181
91
  public string BkColor { get => _BkColor; set => Set(ref _BkColor, value, null); } // 面倒なので全プロパティ発砲
182
-
183
92
  public string _BkColor;
184
93
 
185
-
186
-
187
94
  // 全くイケていないがコンバータを書くより手軽なので。。。
188
-
189
95
  public bool IsRed => BkColor == "Red";
190
-
191
96
  public bool IsYellow => BkColor == "Yellow";
192
-
193
97
  public bool IsWhite => !IsRed && !IsYellow;
194
98
 
99
+ #region INotifyPropertyChanged
100
+ public event PropertyChangedEventHandler PropertyChanged;
101
+ protected bool Set<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
102
+ {
103
+ if(Equals(storage, value)) return false;
104
+ storage = value;
105
+ OnPropertyChanged(propertyName);
106
+ return true;
107
+ }
108
+ protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
109
+ => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
110
+ #endregion
111
+ }
195
112
 
113
+ public partial class MainWindow : Window
114
+ {
115
+ public ObservableCollection<User> Users { get; }
196
116
 
117
+ public MainWindow()
118
+ {
197
- #region INotifyPropertyChanged
119
+ InitializeComponent();
198
120
 
199
- public event PropertyChangedEventHandler PropertyChanged;
121
+ Users = new ObservableCollection<User>
122
+ {
123
+ new User{ No = "WPF C# VS2015", Name = "aaa", Data = "bbb", BkColor = "Red", },
124
+ new User{ No = "WPF C# VS2017", Name = "aaa", Data = "bbb", BkColor = "Yellow", },
125
+ new User{ No = "WPF C# VS2019", Name = "aaa", Data = "bbb", },
126
+ };
200
127
 
201
- protected bool Set<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
202
-
203
- {
204
-
205
- if(Equals(storage, value)) return false;
206
-
207
- storage = value;
128
+ DataContext = this;
208
-
209
- OnPropertyChanged(propertyName);
210
-
211
- return true;
212
-
213
129
  }
214
130
 
215
- protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
131
+ private void MenuItem_Click(object sender, RoutedEventArgs e)
216
-
132
+ {
133
+ if(sender is MenuItem menuItem)
134
+ {
217
- => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
135
+ if(menuItem.DataContext is User user)
218
-
136
+ {
137
+ switch(menuItem.Header)
138
+ {
139
+ case "通常":
140
+ user.BkColor = "";
219
- #endregion
141
+ break;
220
-
142
+ case "黄色":
143
+ user.BkColor = "Yellow";
144
+ break;
145
+ case "赤":
146
+ user.BkColor = "Red";
147
+ break;
148
+ }
149
+ }
150
+ }
151
+ }
221
152
  }
222
-
223
-
224
-
225
- public partial class MainWindow : Window
226
-
227
- {
228
-
229
- public ObservableCollection<User> Users { get; }
230
-
231
-
232
-
233
- public MainWindow()
234
-
235
- {
236
-
237
- InitializeComponent();
238
-
239
-
240
-
241
- Users = new ObservableCollection<User>
242
-
243
- {
244
-
245
- new User{ No = "WPF C# VS2015", Name = "aaa", Data = "bbb", BkColor = "Red", },
246
-
247
- new User{ No = "WPF C# VS2017", Name = "aaa", Data = "bbb", BkColor = "Yellow", },
248
-
249
- new User{ No = "WPF C# VS2019", Name = "aaa", Data = "bbb", },
250
-
251
- };
252
-
253
-
254
-
255
- DataContext = this;
256
-
257
- }
258
-
259
-
260
-
261
- private void MenuItem_Click(object sender, RoutedEventArgs e)
262
-
263
- {
264
-
265
- if(sender is MenuItem menuItem)
266
-
267
- {
268
-
269
- if(menuItem.DataContext is User user)
270
-
271
- {
272
-
273
- switch(menuItem.Header)
274
-
275
- {
276
-
277
- case "通常":
278
-
279
- user.BkColor = "";
280
-
281
- break;
282
-
283
- case "黄色":
284
-
285
- user.BkColor = "Yellow";
286
-
287
- break;
288
-
289
- case "赤":
290
-
291
- user.BkColor = "Red";
292
-
293
- break;
294
-
295
- }
296
-
297
- }
298
-
299
- }
300
-
301
- }
302
-
303
- }
304
-
305
153
  }
306
-
307
154
  ```
308
155
 
309
-
310
-
311
156
  ![アプリ画像](6aae9eb82308358ad7d5fdb3caecbd71.png)