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

回答編集履歴

1

見直しキャンペーン中

2023/07/22 07:06

投稿

TN8001
TN8001

スコア10109

answer CHANGED
@@ -1,222 +1,222 @@
1
- `TextBlock`は軽量コンポーネントとして`FrameworkElement`派生なので、`Template`等がなくあんまり凝ったことはできないです。
2
-
3
- 今の解釈でこういうことかな??というサンプルを作りました。
4
- `Label` `TextBox` `Button`はいろいろ置いてあるよって程度の意味です。
5
- `Canvas`ベースなのも特別意味はありません。
6
-
7
- 添付ビヘイビアはいろいろとガバガバなので、このままでは使い物にならないだろうと思います。
8
-
9
- ```xaml
10
- <Window
11
- x:Class="Questions264652.MainWindow"
12
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
13
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
14
- xmlns:local="clr-namespace:Questions264652"
15
- Width="800"
16
- Height="450">
17
- <Grid>
18
- <Grid.RowDefinitions>
19
- <RowDefinition />
20
- <RowDefinition />
21
- </Grid.RowDefinitions>
22
- <Grid.ColumnDefinitions>
23
- <ColumnDefinition />
24
- <ColumnDefinition />
25
- </Grid.ColumnDefinitions>
26
- <GroupBox Header="今の状態">
27
- <Canvas>
28
- <TextBlock
29
- Canvas.Left="35"
30
- Canvas.Top="24"
31
- Text="ほげほげ" />
32
- <TextBlock
33
- Canvas.Left="177"
34
- Canvas.Top="49"
35
- Text="ふがふが" />
36
- <Label
37
- Canvas.Left="114"
38
- Canvas.Top="122"
39
- Content="Label" />
40
- <TextBox
41
- Canvas.Left="194"
42
- Canvas.Top="126"
43
- Text="TextBox" />
44
- <Button
45
- Canvas.Left="282"
46
- Canvas.Top="125"
47
- Content="Button" />
48
- </Canvas>
49
- </GroupBox>
50
-
51
- <GroupBox Grid.Column="1" Header="こうなるといいなの状態">
52
- <Canvas>
53
- <TextBlock Canvas.Left="35" Canvas.Top="24">
54
- <InlineUIContainer BaselineAlignment="Center">
55
- <Image Width="20" Source="https://teratail-v2.storage.googleapis.com/uploads/avatars/u13/132786/KnkDDC5A_thumbnail_32x32.jpg" />
56
- </InlineUIContainer>
57
- <Run>ほげほげ</Run>
58
- <Bold>!!</Bold>
59
- </TextBlock>
60
- <TextBlock Canvas.Left="177" Canvas.Top="49">
61
- <InlineUIContainer BaselineAlignment="Center">
62
- <Image Width="20" Source="https://teratail-v2.storage.googleapis.com/uploads/avatars/u13/132786/KnkDDC5A_thumbnail_32x32.jpg" />
63
- </InlineUIContainer>
64
- <Run>ふがふが</Run>
65
- <Bold>!!</Bold>
66
- </TextBlock>
67
- <Label
68
- Canvas.Left="114"
69
- Canvas.Top="122"
70
- Content="Label" />
71
- <TextBox
72
- Canvas.Left="194"
73
- Canvas.Top="126"
74
- Text="TextBox" />
75
- <Button
76
- Canvas.Left="282"
77
- Canvas.Top="125"
78
- Content="Button" />
79
- </Canvas>
80
- </GroupBox>
81
-
82
- <GroupBox Grid.Row="1" Header="テンブレート">
83
- <Canvas>
84
- <Canvas.Resources>
85
- <Style TargetType="ContentControl">
86
- <Setter Property="Template">
87
- <Setter.Value>
88
- <ControlTemplate TargetType="ContentControl">
89
- <TextBlock>
90
- <InlineUIContainer BaselineAlignment="Center">
91
- <Image Width="20" Source="https://teratail-v2.storage.googleapis.com/uploads/avatars/u13/132786/KnkDDC5A_thumbnail_32x32.jpg" />
92
- </InlineUIContainer>
93
- <Run Text="{TemplateBinding Content}" />
94
- <Bold>!!</Bold>
95
- </TextBlock>
96
- </ControlTemplate>
97
- </Setter.Value>
98
- </Setter>
99
- </Style>
100
- </Canvas.Resources>
101
- <ContentControl
102
- Canvas.Left="35"
103
- Canvas.Top="24"
104
- Content="ほげほげ" />
105
- <ContentControl
106
- Canvas.Left="177"
107
- Canvas.Top="49"
108
- Content="ふがふが" />
109
- <Label
110
- Canvas.Left="114"
111
- Canvas.Top="122"
112
- Content="Label" />
113
- <TextBox
114
- Canvas.Left="194"
115
- Canvas.Top="126"
116
- Text="TextBox" />
117
- <Button
118
- Canvas.Left="282"
119
- Canvas.Top="125"
120
- Content="Button" />
121
- </Canvas>
122
- </GroupBox>
123
-
124
- <GroupBox
125
- Grid.Row="1"
126
- Grid.Column="1"
127
- Header="添付ビヘイビア">
128
- <Canvas>
129
- <Canvas.Resources>
130
- <InlineUIContainer
131
- x:Key="image"
132
- x:Shared="False"
133
- BaselineAlignment="Center">
134
- <Image Width="20" Source="https://teratail-v2.storage.googleapis.com/uploads/avatars/u13/132786/KnkDDC5A_thumbnail_32x32.jpg" />
135
- </InlineUIContainer>
136
- <Bold x:Key="exclamation" x:Shared="False">!!</Bold>
137
-
138
- <Style TargetType="TextBlock">
139
- <Setter Property="local:AddInlineBehavior.Before" Value="{StaticResource image}" />
140
- <Setter Property="local:AddInlineBehavior.After" Value="{StaticResource exclamation}" />
141
- <Setter Property="local:AddInlineBehavior.Use" Value="True" />
142
- </Style>
143
- </Canvas.Resources>
144
-
145
- <TextBlock
146
- Canvas.Left="35"
147
- Canvas.Top="24"
148
- Text="ほげほげ" />
149
- <TextBlock
150
- Canvas.Left="177"
151
- Canvas.Top="49"
152
- Text="ふがふが" />
153
- <Label
154
- Canvas.Left="114"
155
- Canvas.Top="122"
156
- Content="Label" />
157
- <TextBox
158
- Canvas.Left="194"
159
- Canvas.Top="126"
160
- Text="TextBox" />
161
- <Button
162
- Canvas.Left="282"
163
- Canvas.Top="125"
164
- Content="Button" />
165
- </Canvas>
166
- </GroupBox>
167
- </Grid>
168
- </Window>
169
- ```
170
-
171
- ```C#
172
- using System.Windows;
173
- using System.Windows.Controls;
174
- using System.Windows.Documents;
175
-
176
- namespace Questions264652
177
- {
178
- public class AddInlineBehavior
179
- {
180
- public static DependencyProperty UseProperty
181
- = DependencyProperty.RegisterAttached("Use", typeof(bool), typeof(AddInlineBehavior),
182
- new PropertyMetadata(false, OnUseChanged));
183
- public static void SetUse(DependencyObject obj, bool value) => obj.SetValue(UseProperty, value);
184
- public static bool GetUse(DependencyObject obj) => (bool)obj.GetValue(UseProperty);
185
- private static void OnUseChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
186
- {
187
- if(d is TextBlock textBlock)
188
- {
189
- if((bool)e.NewValue)
190
- {
191
- var tmp = textBlock.Text;
192
- textBlock.Inlines.Clear();
193
- if(GetBefore(textBlock) is Inline before)
194
- {
195
- textBlock.Inlines.Add(before);
196
- textBlock.Inlines.Add(new Run(" "));
197
- }
198
- textBlock.Inlines.Add(new Run(tmp));
199
- if(GetAfter(textBlock) is Inline after)
200
- {
201
- textBlock.Inlines.Add(new Run(" "));
202
- textBlock.Inlines.Add(after);
203
- }
204
- }
205
- }
206
- }
207
-
208
- public static DependencyProperty BeforeProperty
209
- = DependencyProperty.RegisterAttached("Before", typeof(Inline), typeof(AddInlineBehavior),
210
- new PropertyMetadata(null));
211
- public static void SetBefore(DependencyObject obj, Inline value) => obj.SetValue(BeforeProperty, value);
212
- public static Inline GetBefore(DependencyObject obj) => (Inline)obj.GetValue(BeforeProperty);
213
-
214
- public static DependencyProperty AfterProperty
215
- = DependencyProperty.RegisterAttached("After", typeof(Inline), typeof(AddInlineBehavior),
216
- new PropertyMetadata(null));
217
- public static void SetAfter(DependencyObject obj, Inline value) => obj.SetValue(AfterProperty, value);
218
- public static Inline GetAfter(DependencyObject obj) => (Inline)obj.GetValue(AfterProperty);
219
- }
220
- }
221
- ```
1
+ `TextBlock`は軽量コンポーネントとして`FrameworkElement`派生なので、`Template`等がなくあんまり凝ったことはできないです。
2
+
3
+ 今の解釈でこういうことかな??というサンプルを作りました。
4
+ `Label``TextBox``Button`はいろいろ置いてあるよって程度の意味です。
5
+ `Canvas`ベースなのも特別意味はありません。
6
+
7
+ 添付ビヘイビアはいろいろとガバガバなので、このままでは使い物にならないだろうと思います。
8
+
9
+ ```xml
10
+ <Window
11
+ x:Class="Questions264652.MainWindow"
12
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
13
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
14
+ xmlns:local="clr-namespace:Questions264652"
15
+ Width="800"
16
+ Height="450">
17
+ <Grid>
18
+ <Grid.RowDefinitions>
19
+ <RowDefinition />
20
+ <RowDefinition />
21
+ </Grid.RowDefinitions>
22
+ <Grid.ColumnDefinitions>
23
+ <ColumnDefinition />
24
+ <ColumnDefinition />
25
+ </Grid.ColumnDefinitions>
26
+ <GroupBox Header="今の状態">
27
+ <Canvas>
28
+ <TextBlock
29
+ Canvas.Left="35"
30
+ Canvas.Top="24"
31
+ Text="ほげほげ" />
32
+ <TextBlock
33
+ Canvas.Left="177"
34
+ Canvas.Top="49"
35
+ Text="ふがふが" />
36
+ <Label
37
+ Canvas.Left="114"
38
+ Canvas.Top="122"
39
+ Content="Label" />
40
+ <TextBox
41
+ Canvas.Left="194"
42
+ Canvas.Top="126"
43
+ Text="TextBox" />
44
+ <Button
45
+ Canvas.Left="282"
46
+ Canvas.Top="125"
47
+ Content="Button" />
48
+ </Canvas>
49
+ </GroupBox>
50
+
51
+ <GroupBox Grid.Column="1" Header="こうなるといいなの状態">
52
+ <Canvas>
53
+ <TextBlock Canvas.Left="35" Canvas.Top="24">
54
+ <InlineUIContainer BaselineAlignment="Center">
55
+ <Image Width="20" Source="https://teratail-v2.storage.googleapis.com/uploads/avatars/u13/132786/KnkDDC5A_thumbnail_32x32.jpg" />
56
+ </InlineUIContainer>
57
+ <Run>ほげほげ</Run>
58
+ <Bold>!!</Bold>
59
+ </TextBlock>
60
+ <TextBlock Canvas.Left="177" Canvas.Top="49">
61
+ <InlineUIContainer BaselineAlignment="Center">
62
+ <Image Width="20" Source="https://teratail-v2.storage.googleapis.com/uploads/avatars/u13/132786/KnkDDC5A_thumbnail_32x32.jpg" />
63
+ </InlineUIContainer>
64
+ <Run>ふがふが</Run>
65
+ <Bold>!!</Bold>
66
+ </TextBlock>
67
+ <Label
68
+ Canvas.Left="114"
69
+ Canvas.Top="122"
70
+ Content="Label" />
71
+ <TextBox
72
+ Canvas.Left="194"
73
+ Canvas.Top="126"
74
+ Text="TextBox" />
75
+ <Button
76
+ Canvas.Left="282"
77
+ Canvas.Top="125"
78
+ Content="Button" />
79
+ </Canvas>
80
+ </GroupBox>
81
+
82
+ <GroupBox Grid.Row="1" Header="テンブレート">
83
+ <Canvas>
84
+ <Canvas.Resources>
85
+ <Style TargetType="ContentControl">
86
+ <Setter Property="Template">
87
+ <Setter.Value>
88
+ <ControlTemplate TargetType="ContentControl">
89
+ <TextBlock>
90
+ <InlineUIContainer BaselineAlignment="Center">
91
+ <Image Width="20" Source="https://teratail-v2.storage.googleapis.com/uploads/avatars/u13/132786/KnkDDC5A_thumbnail_32x32.jpg" />
92
+ </InlineUIContainer>
93
+ <Run Text="{TemplateBinding Content}" />
94
+ <Bold>!!</Bold>
95
+ </TextBlock>
96
+ </ControlTemplate>
97
+ </Setter.Value>
98
+ </Setter>
99
+ </Style>
100
+ </Canvas.Resources>
101
+ <ContentControl
102
+ Canvas.Left="35"
103
+ Canvas.Top="24"
104
+ Content="ほげほげ" />
105
+ <ContentControl
106
+ Canvas.Left="177"
107
+ Canvas.Top="49"
108
+ Content="ふがふが" />
109
+ <Label
110
+ Canvas.Left="114"
111
+ Canvas.Top="122"
112
+ Content="Label" />
113
+ <TextBox
114
+ Canvas.Left="194"
115
+ Canvas.Top="126"
116
+ Text="TextBox" />
117
+ <Button
118
+ Canvas.Left="282"
119
+ Canvas.Top="125"
120
+ Content="Button" />
121
+ </Canvas>
122
+ </GroupBox>
123
+
124
+ <GroupBox
125
+ Grid.Row="1"
126
+ Grid.Column="1"
127
+ Header="添付ビヘイビア">
128
+ <Canvas>
129
+ <Canvas.Resources>
130
+ <InlineUIContainer
131
+ x:Key="image"
132
+ x:Shared="False"
133
+ BaselineAlignment="Center">
134
+ <Image Width="20" Source="https://teratail-v2.storage.googleapis.com/uploads/avatars/u13/132786/KnkDDC5A_thumbnail_32x32.jpg" />
135
+ </InlineUIContainer>
136
+ <Bold x:Key="exclamation" x:Shared="False">!!</Bold>
137
+
138
+ <Style TargetType="TextBlock">
139
+ <Setter Property="local:AddInlineBehavior.Before" Value="{StaticResource image}" />
140
+ <Setter Property="local:AddInlineBehavior.After" Value="{StaticResource exclamation}" />
141
+ <Setter Property="local:AddInlineBehavior.Use" Value="True" />
142
+ </Style>
143
+ </Canvas.Resources>
144
+
145
+ <TextBlock
146
+ Canvas.Left="35"
147
+ Canvas.Top="24"
148
+ Text="ほげほげ" />
149
+ <TextBlock
150
+ Canvas.Left="177"
151
+ Canvas.Top="49"
152
+ Text="ふがふが" />
153
+ <Label
154
+ Canvas.Left="114"
155
+ Canvas.Top="122"
156
+ Content="Label" />
157
+ <TextBox
158
+ Canvas.Left="194"
159
+ Canvas.Top="126"
160
+ Text="TextBox" />
161
+ <Button
162
+ Canvas.Left="282"
163
+ Canvas.Top="125"
164
+ Content="Button" />
165
+ </Canvas>
166
+ </GroupBox>
167
+ </Grid>
168
+ </Window>
169
+ ```
170
+
171
+ ```cs
172
+ using System.Windows;
173
+ using System.Windows.Controls;
174
+ using System.Windows.Documents;
175
+
176
+ namespace Questions264652
177
+ {
178
+ public class AddInlineBehavior
179
+ {
180
+ public static DependencyProperty UseProperty
181
+ = DependencyProperty.RegisterAttached("Use", typeof(bool), typeof(AddInlineBehavior),
182
+ new PropertyMetadata(false, OnUseChanged));
183
+ public static void SetUse(DependencyObject obj, bool value) => obj.SetValue(UseProperty, value);
184
+ public static bool GetUse(DependencyObject obj) => (bool)obj.GetValue(UseProperty);
185
+ private static void OnUseChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
186
+ {
187
+ if(d is TextBlock textBlock)
188
+ {
189
+ if((bool)e.NewValue)
190
+ {
191
+ var tmp = textBlock.Text;
192
+ textBlock.Inlines.Clear();
193
+ if(GetBefore(textBlock) is Inline before)
194
+ {
195
+ textBlock.Inlines.Add(before);
196
+ textBlock.Inlines.Add(new Run(" "));
197
+ }
198
+ textBlock.Inlines.Add(new Run(tmp));
199
+ if(GetAfter(textBlock) is Inline after)
200
+ {
201
+ textBlock.Inlines.Add(new Run(" "));
202
+ textBlock.Inlines.Add(after);
203
+ }
204
+ }
205
+ }
206
+ }
207
+
208
+ public static DependencyProperty BeforeProperty
209
+ = DependencyProperty.RegisterAttached("Before", typeof(Inline), typeof(AddInlineBehavior),
210
+ new PropertyMetadata(null));
211
+ public static void SetBefore(DependencyObject obj, Inline value) => obj.SetValue(BeforeProperty, value);
212
+ public static Inline GetBefore(DependencyObject obj) => (Inline)obj.GetValue(BeforeProperty);
213
+
214
+ public static DependencyProperty AfterProperty
215
+ = DependencyProperty.RegisterAttached("After", typeof(Inline), typeof(AddInlineBehavior),
216
+ new PropertyMetadata(null));
217
+ public static void SetAfter(DependencyObject obj, Inline value) => obj.SetValue(AfterProperty, value);
218
+ public static Inline GetAfter(DependencyObject obj) => (Inline)obj.GetValue(AfterProperty);
219
+ }
220
+ }
221
+ ```
222
222
  ![アプリ画像](5483fe382e4c33e0d7c3bf926f56e396.png)