回答編集履歴

1

見直しキャンペーン中

2023/07/23 05:14

投稿

TN8001
TN8001

スコア9884

test CHANGED
@@ -1,285 +1,143 @@
1
1
  `DataTemplateSelector`は、`ViewModel`が継承するようなものではありません(通常`View`層に単体で存在します)
2
2
 
3
-
4
-
5
3
  [データ オブジェクトのプロパティに基づく DataTemplate の選択 - WPF | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/framework/wpf/data/data-templating-overview#choosing-a-datatemplate-based-on-properties-of-the-data-object)
6
-
7
4
  作りがだいぶ違いますが、要は「入力値に対して適当なテンプレートを選択して返す」ってだけです(`IValueConverter`なんかもそう)
8
-
9
5
  [方法: バインドされたデータを変換する - WPF | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/framework/wpf/data/how-to-convert-bound-data)
10
6
 
11
-
12
-
13
7
  参考サイトのものを`Livet`で作るとすると、こんな感じでしょうか(`namespace`ごとにまとめていますが、別ファイルです)
14
-
15
8
  ついでに名前を大文字にする~~しょうもない~~コンバーター例(`UppercaseConverter`)も入れました。
16
9
 
17
-
18
-
19
- ```xaml
10
+ ```xml
20
-
21
11
  <Window
22
-
23
12
  x:Class="Questions289487.Views.MainWindow"
24
-
25
13
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
26
-
27
14
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
28
-
29
15
  xmlns:behaviors="http://schemas.microsoft.com/xaml/behaviors"
30
-
31
16
  xmlns:l="http://schemas.livet-mvvm.net/2011/wpf"
32
-
33
17
  xmlns:v="clr-namespace:Questions289487.Views"
34
-
35
18
  xmlns:vm="clr-namespace:Questions289487.ViewModels"
36
-
37
19
  Width="525"
38
-
39
20
  Height="350">
40
21
 
41
-
42
-
43
22
  <Window.DataContext>
44
-
45
23
  <vm:MainWindowViewModel />
46
-
47
24
  </Window.DataContext>
48
25
 
49
-
50
-
51
26
  <behaviors:Interaction.Triggers>
52
-
53
27
  <behaviors:EventTrigger EventName="ContentRendered">
54
-
55
28
  <l:LivetCallMethodAction MethodName="Initialize" MethodTarget="{Binding}" />
56
-
57
29
  </behaviors:EventTrigger>
58
-
59
30
  <behaviors:EventTrigger EventName="Closed">
60
-
61
31
  <l:DataContextDisposeAction />
62
-
63
32
  </behaviors:EventTrigger>
64
-
65
33
  </behaviors:Interaction.Triggers>
66
34
 
67
-
68
-
69
35
  <Window.Resources>
70
-
71
36
  <DataTemplate x:Key="MyTemplate1">
72
-
73
37
  <TextBlock Background="Aqua" Text="{Binding Name}" />
74
-
38
+ </DataTemplate>
39
+ <DataTemplate x:Key="MyTemplate2">
40
+ <TextBlock Background="Gold" Text="{Binding Name}" />
75
41
  </DataTemplate>
76
42
 
77
- <DataTemplate x:Key="MyTemplate2">
78
-
79
- <TextBlock Background="Gold" Text="{Binding Name}" />
80
-
81
- </DataTemplate>
82
-
83
-
84
-
85
43
  <v:ComboHeaderTemplateSelector
86
-
87
44
  x:Key="MySelector"
88
-
89
45
  Template1="{StaticResource MyTemplate1}"
90
-
91
46
  Template2="{StaticResource MyTemplate2}" />
92
47
 
93
-
94
-
95
48
  <v:UppercaseConverter x:Key="UppercaseConverter" />
96
-
97
49
  </Window.Resources>
98
50
 
99
-
100
-
101
51
  <StackPanel Margin="10">
102
-
103
52
  <ComboBox ItemsSource="{Binding View}">
104
-
105
53
  <ComboBox.GroupStyle>
106
-
107
54
  <GroupStyle HeaderTemplateSelector="{StaticResource MySelector}" />
108
-
109
55
  </ComboBox.GroupStyle>
110
-
111
56
  <ComboBox.ItemTemplate>
112
-
113
57
  <DataTemplate>
114
-
115
58
  <TextBlock Text="{Binding Title, Converter={StaticResource UppercaseConverter}}" />
116
-
117
59
  </DataTemplate>
118
-
119
60
  </ComboBox.ItemTemplate>
120
-
121
61
  </ComboBox>
122
-
123
62
  </StackPanel>
124
-
125
63
  </Window>
126
-
127
64
  ```
128
65
 
66
+ ```cs
67
+ using System.Collections.Generic;
68
+ using System.Windows.Data;
69
+ using Livet;
129
70
 
71
+ namespace Questions289487.ViewModels
72
+ {
73
+ // これはModelsにあるべきもの?まあ本題ではないので。。
74
+ public class ComboItem
75
+ {
76
+ public string Title { get; set; }
77
+ public string Category { get; set; }
78
+ }
130
79
 
80
+ public class MainWindowViewModel : ViewModel
81
+ {
82
+ private ListCollectionView _View;
83
+ public ListCollectionView View
84
+ {
131
- ```C#
85
+ get => _View;
86
+ set => RaisePropertyChangedIfSet(ref _View, value);
87
+ }
132
88
 
89
+ public void Initialize()
90
+ {
91
+ var items = new List<ComboItem>
92
+ {
93
+ new ComboItem() { Title = "ichiro", Category = "CategoryA" },
94
+ new ComboItem() { Title = "jiro", Category = "CategoryA" },
95
+ new ComboItem() { Title = "saburo", Category = "CategoryA" },
96
+ new ComboItem() { Title = "momotaro", Category = "CategoryB" },
97
+ new ComboItem() { Title = "kintaro", Category = "CategoryB" },
98
+ };
133
- using System.Collections.Generic;
99
+ View = new ListCollectionView(items);
100
+ View.GroupDescriptions.Add(new PropertyGroupDescription("Category"));
101
+ }
102
+ }
103
+ }
104
+ ```
134
105
 
106
+ ```cs
107
+ using System;
108
+ using System.Globalization;
109
+ using System.Windows;
110
+ using System.Windows.Controls;
135
111
  using System.Windows.Data;
136
112
 
137
- using Livet;
138
-
139
-
140
-
141
- namespace Questions289487.ViewModels
113
+ namespace Questions289487.Views
142
-
143
114
  {
144
-
145
- // これはModelsにあるべきもの?まあ本題ではないので。。
146
-
147
- public class ComboItem
115
+ public partial class MainWindow : Window
148
-
149
116
  {
150
-
151
- public string Title { get; set; }
152
-
153
- public string Category { get; set; }
117
+ public MainWindow() => InitializeComponent();
154
-
155
118
  }
156
119
 
120
+ public class ComboHeaderTemplateSelector : DataTemplateSelector
121
+ {
122
+ public DataTemplate Template1 { get; set; }
123
+ public DataTemplate Template2 { get; set; }
157
124
 
158
-
159
- public class MainWindowViewModel : ViewModel
125
+ public override DataTemplate SelectTemplate(object item, DependencyObject container)
160
-
161
- {
162
-
163
- private ListCollectionView _View;
164
-
165
- public ListCollectionView View
166
-
167
126
  {
168
-
169
- get => _View;
127
+ var group = (CollectionViewGroup)item;
170
-
171
- set => RaisePropertyChangedIfSet(ref _View, value);
128
+ if("CategoryA" == group.Name.ToString()) return Template1;
172
-
129
+ else return Template2;
173
130
  }
174
-
175
-
176
-
177
- public void Initialize()
178
-
179
- {
180
-
181
- var items = new List<ComboItem>
182
-
183
- {
184
-
185
- new ComboItem() { Title = "ichiro", Category = "CategoryA" },
186
-
187
- new ComboItem() { Title = "jiro", Category = "CategoryA" },
188
-
189
- new ComboItem() { Title = "saburo", Category = "CategoryA" },
190
-
191
- new ComboItem() { Title = "momotaro", Category = "CategoryB" },
192
-
193
- new ComboItem() { Title = "kintaro", Category = "CategoryB" },
194
-
195
- };
196
-
197
- View = new ListCollectionView(items);
198
-
199
- View.GroupDescriptions.Add(new PropertyGroupDescription("Category"));
200
-
201
- }
202
-
203
131
  }
204
132
 
133
+ public class UppercaseConverter : IValueConverter
134
+ {
135
+ public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
136
+ {
137
+ var s = (string)value;
138
+ return s.ToUpper();
139
+ }
140
+ public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => throw new NotImplementedException();
141
+ }
205
142
  }
206
-
207
143
  ```
208
-
209
-
210
-
211
- ```C#
212
-
213
- using System;
214
-
215
- using System.Globalization;
216
-
217
- using System.Windows;
218
-
219
- using System.Windows.Controls;
220
-
221
- using System.Windows.Data;
222
-
223
-
224
-
225
- namespace Questions289487.Views
226
-
227
- {
228
-
229
- public partial class MainWindow : Window
230
-
231
- {
232
-
233
- public MainWindow() => InitializeComponent();
234
-
235
- }
236
-
237
-
238
-
239
- public class ComboHeaderTemplateSelector : DataTemplateSelector
240
-
241
- {
242
-
243
- public DataTemplate Template1 { get; set; }
244
-
245
- public DataTemplate Template2 { get; set; }
246
-
247
-
248
-
249
- public override DataTemplate SelectTemplate(object item, DependencyObject container)
250
-
251
- {
252
-
253
- var group = (CollectionViewGroup)item;
254
-
255
- if("CategoryA" == group.Name.ToString()) return Template1;
256
-
257
- else return Template2;
258
-
259
- }
260
-
261
- }
262
-
263
-
264
-
265
- public class UppercaseConverter : IValueConverter
266
-
267
- {
268
-
269
- public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
270
-
271
- {
272
-
273
- var s = (string)value;
274
-
275
- return s.ToUpper();
276
-
277
- }
278
-
279
- public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => throw new NotImplementedException();
280
-
281
- }
282
-
283
- }
284
-
285
- ```