回答編集履歴

4

見直しキャンペーン中

2023/08/12 09:58

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -1,9 +1,9 @@
1
1
  カスタムコントロールを作ってください。
2
- `UWP`向けの記事があんまり見つからないので、`WPF`で調べてください(試してないですが流れは同じはずです^^;
2
+ UWP向けの記事があんまり見つからないので、WPFで調べてください ~~(試してないですが流れは同じはずです^^;~~
3
3
 
4
4
  ---
5
5
 
6
- `UWP`では「**テンプレート コントロール**」という用語になっているようです(コントロールテンプレートだとまた別の意味になってしまいます)
6
+ UWPでは「**テンプレート コントロール**」という用語になっているようです(コントロールテンプレートだとまた別の意味になってしまいます)
7
7
  ![イメージ説明](743ed00f415c708a8725ff44892a6aa7.png)
8
8
  右の説明やファイル名はカスタムコントロールなのに。。。
9
9
 
@@ -26,7 +26,7 @@
26
26
  [WPF4.5入門 その54 「カスタムコントロール」 - かずきのBlog@hatena](https://blog.okazuki.jp/entry/2014/09/08/221209)
27
27
 
28
28
 
29
- ### App1プロジェクト
29
+ ##### App1プロジェクト
30
30
  ```xml:MainPage.xaml
31
31
  <Page
32
32
  x:Class="App1.MainPage"
@@ -64,7 +64,7 @@
64
64
  </Page>
65
65
  ```
66
66
 
67
- ### ClassLibrary1プロジェクト
67
+ ##### ClassLibrary1プロジェクト
68
68
  ```xml:Generic.xaml
69
69
  <ResourceDictionary
70
70
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
@@ -157,4 +157,5 @@
157
157
  }
158
158
  ```
159
159
  他は初期状態
160
+
160
161
  ![イメージ説明](254e5b26f7a426dba70b2d4d29e9ab1b.png)

3

見直しキャンペーン中

2023/07/23 06:25

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -1,321 +1,160 @@
1
1
  カスタムコントロールを作ってください。
2
-
3
2
  `UWP`向けの記事があんまり見つからないので、`WPF`で調べてください(試してないですが流れは同じはずです^^;
4
-
5
-
6
3
 
7
4
  ---
8
5
 
9
-
10
-
11
6
  `UWP`では「**テンプレート コントロール**」という用語になっているようです(コントロールテンプレートだとまた別の意味になってしまいます)
12
-
13
7
  ![イメージ説明](743ed00f415c708a8725ff44892a6aa7.png)
14
-
15
8
  右の説明やファイル名はカスタムコントロールなのに。。。
16
9
 
17
-
18
-
19
10
  [Windows ストア アプリでページ間で共通の見た目を簡単に作りたい - かずきのBlog@hatena](https://blog.okazuki.jp/entry/2014/01/09/003357)
20
-
21
11
  ちょっと例を書いてみようと思ったのですが、↑で十分でしょうか。
22
12
 
23
-
24
-
25
13
  [C++/WinRT による XAML カスタム (テンプレート化) コントロール - UWP applications | Microsoft Docs](https://docs.microsoft.com/ja-jp/windows/uwp/cpp-and-winrt-apis/xaml-cust-ctrl)
26
-
27
14
  公式での説明はC++での例しか見つけられませんでした。
28
-
29
-
30
15
 
31
16
  ---
32
17
 
33
-
34
-
35
18
  > 見た目や処理が同じPageが多々でてきますので、
36
-
37
19
  > なにかベースになるものを用意し、
38
-
39
20
  > そこから派生して、
40
-
41
21
  > オリジナルのコンポーネントを作成
42
-
43
-
44
22
 
45
23
  には全く役に立ちませんが、テンプレート コントロールの検証です。
46
24
 
47
-
48
-
49
25
  なんかネタが思いつかなかったので、またかずきさんのお借りしました^^;
50
-
51
26
  [WPF4.5入門 その54 「カスタムコントロール」 - かずきのBlog@hatena](https://blog.okazuki.jp/entry/2014/09/08/221209)
52
27
 
53
28
 
54
-
29
+ ### App1プロジェクト
55
-
56
-
57
- App1プロジェクト MainPage.xaml
30
+ ```xml:MainPage.xaml
58
-
59
- ```xaml
60
-
61
31
  <Page
62
-
63
32
  x:Class="App1.MainPage"
64
-
65
33
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
66
-
67
34
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
68
-
69
35
  xmlns:classLibrary1="using:ClassLibrary1"
70
-
71
36
  Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
72
37
 
73
-
74
-
75
38
  <StackPanel>
76
-
77
39
  <classLibrary1:NumericUpDown
78
-
79
40
  HorizontalAlignment="Left"
80
-
81
41
  Background="LightBlue"
82
-
83
42
  BorderBrush="Green"
84
-
85
43
  BorderThickness="2"
86
-
87
44
  FontSize="36"
88
-
89
45
  Foreground="Red"
90
-
91
46
  Value="100" />
92
47
 
93
-
94
-
95
48
  <classLibrary1:NumericUpDown HorizontalAlignment="Left">
96
-
97
49
  <classLibrary1:NumericUpDown.Template>
98
-
99
50
  <ControlTemplate TargetType="classLibrary1:NumericUpDown">
100
-
101
51
  <StackPanel>
102
-
103
52
  <RepeatButton x:Name="PART_UpButton" HorizontalAlignment="Stretch">
104
-
105
53
  <FontIcon FontFamily="{StaticResource SymbolThemeFontFamily}" Glyph="&#xE70E;" />
106
-
107
54
  </RepeatButton>
108
-
109
55
  <TextBlock HorizontalAlignment="Center" Text="{Binding Value, RelativeSource={RelativeSource TemplatedParent}}" />
110
-
111
56
  <RepeatButton x:Name="PART_DownButton" HorizontalAlignment="Stretch">
112
-
113
57
  <FontIcon FontFamily="{StaticResource SymbolThemeFontFamily}" Glyph="&#xE70D;" />
114
-
115
58
  </RepeatButton>
116
-
117
59
  </StackPanel>
118
-
119
60
  </ControlTemplate>
120
-
121
61
  </classLibrary1:NumericUpDown.Template>
122
-
123
62
  </classLibrary1:NumericUpDown>
124
-
125
63
  </StackPanel>
126
-
127
64
  </Page>
128
-
129
65
  ```
130
66
 
131
-
132
-
133
- ClassLibrary1プロジェクト Generic.xaml
67
+ ### ClassLibrary1プロジェクト
134
-
135
- ```xaml
68
+ ```xml:Generic.xaml
136
-
137
69
  <ResourceDictionary
138
-
139
70
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
140
-
141
71
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
142
-
143
72
  xmlns:local="using:ClassLibrary1">
144
-
145
73
  <Style TargetType="local:NumericUpDown">
146
-
147
74
  <Setter Property="Template">
148
-
149
75
  <Setter.Value>
150
-
151
76
  <ControlTemplate TargetType="local:NumericUpDown">
152
-
153
77
  <Border
154
-
155
78
  Background="{TemplateBinding Background}"
156
-
157
79
  BorderBrush="{TemplateBinding BorderBrush}"
158
-
159
80
  BorderThickness="{TemplateBinding BorderThickness}">
160
-
161
81
  <Grid>
162
-
163
82
  <Grid.RowDefinitions>
164
-
165
83
  <RowDefinition />
166
-
167
84
  <RowDefinition />
168
-
169
85
  </Grid.RowDefinitions>
170
-
171
86
  <Grid.ColumnDefinitions>
172
-
173
87
  <ColumnDefinition />
174
-
175
88
  <ColumnDefinition />
176
-
177
89
  </Grid.ColumnDefinitions>
178
-
179
90
  <TextBlock
180
-
181
91
  Grid.RowSpan="2"
182
-
183
92
  VerticalAlignment="Center"
184
-
185
93
  Foreground="{TemplateBinding Foreground}"
186
-
187
94
  Text="{Binding Value, RelativeSource={RelativeSource TemplatedParent}}"
188
-
189
95
  TextWrapping="Wrap" />
190
-
191
96
  <RepeatButton
192
-
193
97
  x:Name="PART_UpButton"
194
-
195
98
  Grid.Column="1"
196
-
197
99
  HorizontalAlignment="Stretch"
198
-
199
100
  Content="Up" />
200
-
201
101
  <RepeatButton
202
-
203
102
  x:Name="PART_DownButton"
204
-
205
103
  Grid.Row="1"
206
-
207
104
  Grid.Column="1"
208
-
209
105
  HorizontalAlignment="Stretch"
210
-
211
106
  Content="Down" />
212
-
213
107
  </Grid>
214
-
215
108
  </Border>
216
-
217
109
  </ControlTemplate>
218
-
219
110
  </Setter.Value>
220
-
221
111
  </Setter>
222
-
223
112
  </Style>
224
-
225
113
  </ResourceDictionary>
226
-
227
114
  ```
228
115
 
229
-
230
-
231
- ClassLibrary1プロジェクト NumericUpDown.cs
116
+ ```cs:NumericUpDown.cs
232
-
233
- ```C#
234
-
235
117
  using Windows.UI.Xaml;
236
-
237
118
  using Windows.UI.Xaml.Controls;
238
-
239
119
  using Windows.UI.Xaml.Controls.Primitives;
240
120
 
241
121
 
242
-
243
-
244
-
245
122
  namespace ClassLibrary1
246
-
247
123
  {
248
-
249
124
  public sealed class NumericUpDown : Control
250
-
251
125
  {
252
-
253
126
  public static readonly DependencyProperty ValueProperty
254
-
255
127
  = DependencyProperty.Register(nameof(Value), typeof(int), typeof(NumericUpDown), new PropertyMetadata(0));
256
-
257
128
  public int Value
258
-
259
129
  {
260
-
261
130
  get => (int)GetValue(ValueProperty);
262
-
263
131
  set => SetValue(ValueProperty, value);
264
-
265
132
  }
266
133
 
267
-
268
-
269
134
  private RepeatButton upButton;
270
-
271
135
  private RepeatButton downButton;
272
-
273
-
274
136
 
275
137
  public NumericUpDown() => DefaultStyleKey = typeof(NumericUpDown);
276
138
 
277
139
 
278
-
279
-
280
-
281
140
  private void UpClick(object sender, RoutedEventArgs e) => Value++;
282
-
283
141
  private void DownClick(object sender, RoutedEventArgs e) => Value--;
284
142
 
285
-
286
-
287
143
  protected override void OnApplyTemplate()
288
-
289
144
  {
290
-
291
145
  base.OnApplyTemplate();
292
146
 
293
-
294
-
295
147
  if(upButton != null) upButton.Click -= UpClick;
296
-
297
148
  if(downButton != null) downButton.Click -= DownClick;
298
149
 
299
-
300
-
301
150
  upButton = GetTemplateChild("PART_UpButton") as RepeatButton;
302
-
303
151
  downButton = GetTemplateChild("PART_DownButton") as RepeatButton;
304
152
 
305
-
306
-
307
153
  if(upButton != null) upButton.Click += UpClick;
308
-
309
154
  if(downButton != null) downButton.Click += DownClick;
310
-
311
155
  }
312
-
313
156
  }
314
-
315
157
  }
316
-
317
158
  ```
318
-
319
159
  他は初期状態
320
-
321
160
  ![イメージ説明](254e5b26f7a426dba70b2d4d29e9ab1b.png)

2

検証追記

2020/09/23 10:00

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -25,3 +25,297 @@
25
25
  [C++/WinRT による XAML カスタム (テンプレート化) コントロール - UWP applications | Microsoft Docs](https://docs.microsoft.com/ja-jp/windows/uwp/cpp-and-winrt-apis/xaml-cust-ctrl)
26
26
 
27
27
  公式での説明はC++での例しか見つけられませんでした。
28
+
29
+
30
+
31
+ ---
32
+
33
+
34
+
35
+ > 見た目や処理が同じPageが多々でてきますので、
36
+
37
+ > なにかベースになるものを用意し、
38
+
39
+ > そこから派生して、
40
+
41
+ > オリジナルのコンポーネントを作成
42
+
43
+
44
+
45
+ には全く役に立ちませんが、テンプレート コントロールの検証です。
46
+
47
+
48
+
49
+ なんかネタが思いつかなかったので、またかずきさんのお借りしました^^;
50
+
51
+ [WPF4.5入門 その54 「カスタムコントロール」 - かずきのBlog@hatena](https://blog.okazuki.jp/entry/2014/09/08/221209)
52
+
53
+
54
+
55
+
56
+
57
+ App1プロジェクト MainPage.xaml
58
+
59
+ ```xaml
60
+
61
+ <Page
62
+
63
+ x:Class="App1.MainPage"
64
+
65
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
66
+
67
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
68
+
69
+ xmlns:classLibrary1="using:ClassLibrary1"
70
+
71
+ Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
72
+
73
+
74
+
75
+ <StackPanel>
76
+
77
+ <classLibrary1:NumericUpDown
78
+
79
+ HorizontalAlignment="Left"
80
+
81
+ Background="LightBlue"
82
+
83
+ BorderBrush="Green"
84
+
85
+ BorderThickness="2"
86
+
87
+ FontSize="36"
88
+
89
+ Foreground="Red"
90
+
91
+ Value="100" />
92
+
93
+
94
+
95
+ <classLibrary1:NumericUpDown HorizontalAlignment="Left">
96
+
97
+ <classLibrary1:NumericUpDown.Template>
98
+
99
+ <ControlTemplate TargetType="classLibrary1:NumericUpDown">
100
+
101
+ <StackPanel>
102
+
103
+ <RepeatButton x:Name="PART_UpButton" HorizontalAlignment="Stretch">
104
+
105
+ <FontIcon FontFamily="{StaticResource SymbolThemeFontFamily}" Glyph="&#xE70E;" />
106
+
107
+ </RepeatButton>
108
+
109
+ <TextBlock HorizontalAlignment="Center" Text="{Binding Value, RelativeSource={RelativeSource TemplatedParent}}" />
110
+
111
+ <RepeatButton x:Name="PART_DownButton" HorizontalAlignment="Stretch">
112
+
113
+ <FontIcon FontFamily="{StaticResource SymbolThemeFontFamily}" Glyph="&#xE70D;" />
114
+
115
+ </RepeatButton>
116
+
117
+ </StackPanel>
118
+
119
+ </ControlTemplate>
120
+
121
+ </classLibrary1:NumericUpDown.Template>
122
+
123
+ </classLibrary1:NumericUpDown>
124
+
125
+ </StackPanel>
126
+
127
+ </Page>
128
+
129
+ ```
130
+
131
+
132
+
133
+ ClassLibrary1プロジェクト Generic.xaml
134
+
135
+ ```xaml
136
+
137
+ <ResourceDictionary
138
+
139
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
140
+
141
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
142
+
143
+ xmlns:local="using:ClassLibrary1">
144
+
145
+ <Style TargetType="local:NumericUpDown">
146
+
147
+ <Setter Property="Template">
148
+
149
+ <Setter.Value>
150
+
151
+ <ControlTemplate TargetType="local:NumericUpDown">
152
+
153
+ <Border
154
+
155
+ Background="{TemplateBinding Background}"
156
+
157
+ BorderBrush="{TemplateBinding BorderBrush}"
158
+
159
+ BorderThickness="{TemplateBinding BorderThickness}">
160
+
161
+ <Grid>
162
+
163
+ <Grid.RowDefinitions>
164
+
165
+ <RowDefinition />
166
+
167
+ <RowDefinition />
168
+
169
+ </Grid.RowDefinitions>
170
+
171
+ <Grid.ColumnDefinitions>
172
+
173
+ <ColumnDefinition />
174
+
175
+ <ColumnDefinition />
176
+
177
+ </Grid.ColumnDefinitions>
178
+
179
+ <TextBlock
180
+
181
+ Grid.RowSpan="2"
182
+
183
+ VerticalAlignment="Center"
184
+
185
+ Foreground="{TemplateBinding Foreground}"
186
+
187
+ Text="{Binding Value, RelativeSource={RelativeSource TemplatedParent}}"
188
+
189
+ TextWrapping="Wrap" />
190
+
191
+ <RepeatButton
192
+
193
+ x:Name="PART_UpButton"
194
+
195
+ Grid.Column="1"
196
+
197
+ HorizontalAlignment="Stretch"
198
+
199
+ Content="Up" />
200
+
201
+ <RepeatButton
202
+
203
+ x:Name="PART_DownButton"
204
+
205
+ Grid.Row="1"
206
+
207
+ Grid.Column="1"
208
+
209
+ HorizontalAlignment="Stretch"
210
+
211
+ Content="Down" />
212
+
213
+ </Grid>
214
+
215
+ </Border>
216
+
217
+ </ControlTemplate>
218
+
219
+ </Setter.Value>
220
+
221
+ </Setter>
222
+
223
+ </Style>
224
+
225
+ </ResourceDictionary>
226
+
227
+ ```
228
+
229
+
230
+
231
+ ClassLibrary1プロジェクト NumericUpDown.cs
232
+
233
+ ```C#
234
+
235
+ using Windows.UI.Xaml;
236
+
237
+ using Windows.UI.Xaml.Controls;
238
+
239
+ using Windows.UI.Xaml.Controls.Primitives;
240
+
241
+
242
+
243
+
244
+
245
+ namespace ClassLibrary1
246
+
247
+ {
248
+
249
+ public sealed class NumericUpDown : Control
250
+
251
+ {
252
+
253
+ public static readonly DependencyProperty ValueProperty
254
+
255
+ = DependencyProperty.Register(nameof(Value), typeof(int), typeof(NumericUpDown), new PropertyMetadata(0));
256
+
257
+ public int Value
258
+
259
+ {
260
+
261
+ get => (int)GetValue(ValueProperty);
262
+
263
+ set => SetValue(ValueProperty, value);
264
+
265
+ }
266
+
267
+
268
+
269
+ private RepeatButton upButton;
270
+
271
+ private RepeatButton downButton;
272
+
273
+
274
+
275
+ public NumericUpDown() => DefaultStyleKey = typeof(NumericUpDown);
276
+
277
+
278
+
279
+
280
+
281
+ private void UpClick(object sender, RoutedEventArgs e) => Value++;
282
+
283
+ private void DownClick(object sender, RoutedEventArgs e) => Value--;
284
+
285
+
286
+
287
+ protected override void OnApplyTemplate()
288
+
289
+ {
290
+
291
+ base.OnApplyTemplate();
292
+
293
+
294
+
295
+ if(upButton != null) upButton.Click -= UpClick;
296
+
297
+ if(downButton != null) downButton.Click -= DownClick;
298
+
299
+
300
+
301
+ upButton = GetTemplateChild("PART_UpButton") as RepeatButton;
302
+
303
+ downButton = GetTemplateChild("PART_DownButton") as RepeatButton;
304
+
305
+
306
+
307
+ if(upButton != null) upButton.Click += UpClick;
308
+
309
+ if(downButton != null) downButton.Click += DownClick;
310
+
311
+ }
312
+
313
+ }
314
+
315
+ }
316
+
317
+ ```
318
+
319
+ 他は初期状態
320
+
321
+ ![イメージ説明](254e5b26f7a426dba70b2d4d29e9ab1b.png)

1

用語追記

2020/09/23 09:59

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -1,3 +1,27 @@
1
1
  カスタムコントロールを作ってください。
2
2
 
3
3
  `UWP`向けの記事があんまり見つからないので、`WPF`で調べてください(試してないですが流れは同じはずです^^;
4
+
5
+
6
+
7
+ ---
8
+
9
+
10
+
11
+ `UWP`では「**テンプレート コントロール**」という用語になっているようです(コントロールテンプレートだとまた別の意味になってしまいます)
12
+
13
+ ![イメージ説明](743ed00f415c708a8725ff44892a6aa7.png)
14
+
15
+ 右の説明やファイル名はカスタムコントロールなのに。。。
16
+
17
+
18
+
19
+ [Windows ストア アプリでページ間で共通の見た目を簡単に作りたい - かずきのBlog@hatena](https://blog.okazuki.jp/entry/2014/01/09/003357)
20
+
21
+ ちょっと例を書いてみようと思ったのですが、↑で十分でしょうか。
22
+
23
+
24
+
25
+ [C++/WinRT による XAML カスタム (テンプレート化) コントロール - UWP applications | Microsoft Docs](https://docs.microsoft.com/ja-jp/windows/uwp/cpp-and-winrt-apis/xaml-cust-ctrl)
26
+
27
+ 公式での説明はC++での例しか見つけられませんでした。