回答編集履歴

1

見直しキャンペーン中

2023/07/22 07:06

投稿

TN8001
TN8001

スコア9357

test CHANGED
@@ -1,443 +1,222 @@
1
1
  `TextBlock`は軽量コンポーネントとして`FrameworkElement`派生なので、`Template`等がなくあんまり凝ったことはできないです。
2
2
 
3
-
4
-
5
3
  今の解釈でこういうことかな??というサンプルを作りました。
6
-
7
- `Label` `TextBox` `Button`はいろいろ置いてあるよって程度の意味です。
4
+ `Label``TextBox``Button`はいろいろ置いてあるよって程度の意味です。
8
-
9
5
  `Canvas`ベースなのも特別意味はありません。
10
6
 
11
-
12
-
13
7
  添付ビヘイビアはいろいろとガバガバなので、このままでは使い物にならないだろうと思います。
14
8
 
15
-
16
-
17
- ```xaml
9
+ ```xml
18
-
19
10
  <Window
20
-
21
11
  x:Class="Questions264652.MainWindow"
22
-
23
12
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
24
-
25
13
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
26
-
27
14
  xmlns:local="clr-namespace:Questions264652"
28
-
29
15
  Width="800"
30
-
31
16
  Height="450">
32
-
33
17
  <Grid>
34
-
35
18
  <Grid.RowDefinitions>
36
-
37
19
  <RowDefinition />
38
-
39
20
  <RowDefinition />
40
-
41
21
  </Grid.RowDefinitions>
42
-
43
22
  <Grid.ColumnDefinitions>
44
-
45
23
  <ColumnDefinition />
46
-
47
24
  <ColumnDefinition />
48
-
49
25
  </Grid.ColumnDefinitions>
50
-
51
26
  <GroupBox Header="今の状態">
52
-
53
- <Canvas>
27
+ <Canvas>
54
-
55
- <TextBlock
28
+ <TextBlock
56
-
57
29
  Canvas.Left="35"
58
-
59
30
  Canvas.Top="24"
60
-
61
31
  Text="ほげほげ" />
62
-
63
- <TextBlock
32
+ <TextBlock
64
-
65
33
  Canvas.Left="177"
66
-
67
34
  Canvas.Top="49"
68
-
69
35
  Text="ふがふが" />
70
-
71
- <Label
36
+ <Label
72
-
73
- Canvas.Left="114"
37
+ Canvas.Left="114"
74
-
75
- Canvas.Top="122"
38
+ Canvas.Top="122"
76
-
77
- Content="Label" />
39
+ Content="Label" />
78
-
79
- <TextBox
40
+ <TextBox
80
-
81
- Canvas.Left="194"
41
+ Canvas.Left="194"
82
-
83
- Canvas.Top="126"
42
+ Canvas.Top="126"
84
-
85
- Text="TextBox" />
43
+ Text="TextBox" />
86
-
87
- <Button
44
+ <Button
88
-
89
- Canvas.Left="282"
45
+ Canvas.Left="282"
90
-
91
- Canvas.Top="125"
46
+ Canvas.Top="125"
92
-
93
- Content="Button" />
47
+ Content="Button" />
94
-
95
- </Canvas>
48
+ </Canvas>
96
-
97
- </GroupBox>
49
+ </GroupBox>
98
-
99
50
 
100
-
101
51
  <GroupBox Grid.Column="1" Header="こうなるといいなの状態">
102
-
103
- <Canvas>
52
+ <Canvas>
104
-
105
53
  <TextBlock Canvas.Left="35" Canvas.Top="24">
106
-
107
54
  <InlineUIContainer BaselineAlignment="Center">
108
-
109
55
  <Image Width="20" Source="https://teratail-v2.storage.googleapis.com/uploads/avatars/u13/132786/KnkDDC5A_thumbnail_32x32.jpg" />
110
-
111
56
  </InlineUIContainer>
112
-
113
57
  <Run>ほげほげ</Run>
114
-
115
58
  <Bold>!!</Bold>
116
-
117
59
  </TextBlock>
118
-
119
60
  <TextBlock Canvas.Left="177" Canvas.Top="49">
120
-
121
61
  <InlineUIContainer BaselineAlignment="Center">
122
-
123
62
  <Image Width="20" Source="https://teratail-v2.storage.googleapis.com/uploads/avatars/u13/132786/KnkDDC5A_thumbnail_32x32.jpg" />
124
-
125
63
  </InlineUIContainer>
126
-
127
64
  <Run>ふがふが</Run>
128
-
129
65
  <Bold>!!</Bold>
130
-
131
66
  </TextBlock>
132
-
133
- <Label
67
+ <Label
134
-
135
- Canvas.Left="114"
68
+ Canvas.Left="114"
136
-
137
- Canvas.Top="122"
69
+ Canvas.Top="122"
138
-
139
- Content="Label" />
70
+ Content="Label" />
140
-
141
- <TextBox
71
+ <TextBox
142
-
143
- Canvas.Left="194"
72
+ Canvas.Left="194"
144
-
145
- Canvas.Top="126"
73
+ Canvas.Top="126"
146
-
147
- Text="TextBox" />
74
+ Text="TextBox" />
148
-
149
- <Button
75
+ <Button
150
-
151
- Canvas.Left="282"
76
+ Canvas.Left="282"
152
-
153
- Canvas.Top="125"
77
+ Canvas.Top="125"
154
-
155
- Content="Button" />
78
+ Content="Button" />
156
-
157
- </Canvas>
79
+ </Canvas>
158
-
159
- </GroupBox>
80
+ </GroupBox>
160
-
161
81
 
162
-
163
82
  <GroupBox Grid.Row="1" Header="テンブレート">
164
-
165
- <Canvas>
83
+ <Canvas>
166
-
167
84
  <Canvas.Resources>
168
-
169
85
  <Style TargetType="ContentControl">
170
-
171
86
  <Setter Property="Template">
172
-
173
87
  <Setter.Value>
174
-
175
88
  <ControlTemplate TargetType="ContentControl">
176
-
177
89
  <TextBlock>
178
-
179
90
  <InlineUIContainer BaselineAlignment="Center">
180
-
181
91
  <Image Width="20" Source="https://teratail-v2.storage.googleapis.com/uploads/avatars/u13/132786/KnkDDC5A_thumbnail_32x32.jpg" />
182
-
183
92
  </InlineUIContainer>
184
-
185
93
  <Run Text="{TemplateBinding Content}" />
186
-
187
94
  <Bold>!!</Bold>
188
-
189
95
  </TextBlock>
190
-
191
96
  </ControlTemplate>
192
-
193
97
  </Setter.Value>
194
-
195
98
  </Setter>
196
-
197
99
  </Style>
198
-
199
100
  </Canvas.Resources>
200
-
201
101
  <ContentControl
202
-
203
102
  Canvas.Left="35"
204
-
205
103
  Canvas.Top="24"
206
-
207
104
  Content="ほげほげ" />
208
-
209
105
  <ContentControl
210
-
211
106
  Canvas.Left="177"
212
-
213
107
  Canvas.Top="49"
214
-
215
108
  Content="ふがふが" />
216
-
217
- <Label
109
+ <Label
218
-
219
- Canvas.Left="114"
110
+ Canvas.Left="114"
220
-
221
- Canvas.Top="122"
111
+ Canvas.Top="122"
222
-
223
- Content="Label" />
112
+ Content="Label" />
224
-
225
- <TextBox
113
+ <TextBox
226
-
227
- Canvas.Left="194"
114
+ Canvas.Left="194"
228
-
229
- Canvas.Top="126"
115
+ Canvas.Top="126"
230
-
231
- Text="TextBox" />
116
+ Text="TextBox" />
232
-
233
- <Button
117
+ <Button
234
-
235
- Canvas.Left="282"
118
+ Canvas.Left="282"
236
-
237
- Canvas.Top="125"
119
+ Canvas.Top="125"
238
-
239
- Content="Button" />
120
+ Content="Button" />
240
-
241
- </Canvas>
121
+ </Canvas>
242
-
243
- </GroupBox>
122
+ </GroupBox>
244
-
245
123
 
246
-
247
124
  <GroupBox
248
-
249
125
  Grid.Row="1"
250
-
251
126
  Grid.Column="1"
252
-
253
127
  Header="添付ビヘイビア">
254
-
255
- <Canvas>
128
+ <Canvas>
256
-
257
129
  <Canvas.Resources>
258
-
259
130
  <InlineUIContainer
260
-
261
131
  x:Key="image"
262
-
263
132
  x:Shared="False"
264
-
265
133
  BaselineAlignment="Center">
266
-
267
134
  <Image Width="20" Source="https://teratail-v2.storage.googleapis.com/uploads/avatars/u13/132786/KnkDDC5A_thumbnail_32x32.jpg" />
268
-
269
135
  </InlineUIContainer>
270
-
271
136
  <Bold x:Key="exclamation" x:Shared="False">!!</Bold>
272
137
 
273
-
274
-
275
138
  <Style TargetType="TextBlock">
276
-
277
139
  <Setter Property="local:AddInlineBehavior.Before" Value="{StaticResource image}" />
278
-
279
140
  <Setter Property="local:AddInlineBehavior.After" Value="{StaticResource exclamation}" />
280
-
281
141
  <Setter Property="local:AddInlineBehavior.Use" Value="True" />
282
-
283
142
  </Style>
284
-
285
143
  </Canvas.Resources>
286
144
 
287
-
288
-
289
- <TextBlock
145
+ <TextBlock
290
-
291
146
  Canvas.Left="35"
292
-
293
147
  Canvas.Top="24"
294
-
295
148
  Text="ほげほげ" />
296
-
297
- <TextBlock
149
+ <TextBlock
298
-
299
150
  Canvas.Left="177"
300
-
301
151
  Canvas.Top="49"
302
-
303
152
  Text="ふがふが" />
304
-
305
- <Label
153
+ <Label
306
-
307
- Canvas.Left="114"
154
+ Canvas.Left="114"
308
-
309
- Canvas.Top="122"
155
+ Canvas.Top="122"
310
-
311
- Content="Label" />
156
+ Content="Label" />
312
-
313
- <TextBox
157
+ <TextBox
314
-
315
- Canvas.Left="194"
158
+ Canvas.Left="194"
316
-
317
- Canvas.Top="126"
159
+ Canvas.Top="126"
318
-
319
- Text="TextBox" />
160
+ Text="TextBox" />
320
-
321
- <Button
161
+ <Button
322
-
323
- Canvas.Left="282"
162
+ Canvas.Left="282"
324
-
325
- Canvas.Top="125"
163
+ Canvas.Top="125"
326
-
327
- Content="Button" />
164
+ Content="Button" />
328
-
329
- </Canvas>
165
+ </Canvas>
330
-
331
- </GroupBox>
166
+ </GroupBox>
332
-
333
167
  </Grid>
334
-
335
168
  </Window>
336
-
337
169
  ```
338
170
 
339
-
340
-
341
- ```C#
171
+ ```cs
342
-
343
172
  using System.Windows;
344
-
345
173
  using System.Windows.Controls;
346
-
347
174
  using System.Windows.Documents;
348
175
 
349
-
350
-
351
176
  namespace Questions264652
352
-
353
177
  {
354
-
355
178
  public class AddInlineBehavior
356
-
357
179
  {
358
-
359
180
  public static DependencyProperty UseProperty
360
-
361
181
  = DependencyProperty.RegisterAttached("Use", typeof(bool), typeof(AddInlineBehavior),
362
-
363
182
  new PropertyMetadata(false, OnUseChanged));
364
-
365
183
  public static void SetUse(DependencyObject obj, bool value) => obj.SetValue(UseProperty, value);
366
-
367
184
  public static bool GetUse(DependencyObject obj) => (bool)obj.GetValue(UseProperty);
368
-
369
185
  private static void OnUseChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
370
-
371
186
  {
372
-
373
187
  if(d is TextBlock textBlock)
374
-
375
188
  {
376
-
377
189
  if((bool)e.NewValue)
378
-
379
190
  {
380
-
381
191
  var tmp = textBlock.Text;
382
-
383
192
  textBlock.Inlines.Clear();
384
-
385
193
  if(GetBefore(textBlock) is Inline before)
386
-
387
194
  {
388
-
389
195
  textBlock.Inlines.Add(before);
390
-
391
196
  textBlock.Inlines.Add(new Run(" "));
392
-
393
197
  }
394
-
395
198
  textBlock.Inlines.Add(new Run(tmp));
396
-
397
199
  if(GetAfter(textBlock) is Inline after)
398
-
399
200
  {
400
-
401
201
  textBlock.Inlines.Add(new Run(" "));
402
-
403
202
  textBlock.Inlines.Add(after);
404
-
405
203
  }
406
-
407
204
  }
408
-
409
205
  }
410
-
411
206
  }
412
207
 
413
-
414
-
415
208
  public static DependencyProperty BeforeProperty
416
-
417
209
  = DependencyProperty.RegisterAttached("Before", typeof(Inline), typeof(AddInlineBehavior),
418
-
419
210
  new PropertyMetadata(null));
420
-
421
211
  public static void SetBefore(DependencyObject obj, Inline value) => obj.SetValue(BeforeProperty, value);
422
-
423
212
  public static Inline GetBefore(DependencyObject obj) => (Inline)obj.GetValue(BeforeProperty);
424
213
 
425
-
426
-
427
214
  public static DependencyProperty AfterProperty
428
-
429
215
  = DependencyProperty.RegisterAttached("After", typeof(Inline), typeof(AddInlineBehavior),
430
-
431
216
  new PropertyMetadata(null));
432
-
433
217
  public static void SetAfter(DependencyObject obj, Inline value) => obj.SetValue(AfterProperty, value);
434
-
435
218
  public static Inline GetAfter(DependencyObject obj) => (Inline)obj.GetValue(AfterProperty);
436
-
437
219
  }
438
-
439
220
  }
440
-
441
221
  ```
442
-
443
222
  ![アプリ画像](5483fe382e4c33e0d7c3bf926f56e396.png)