回答編集履歴

1

見直しキャンペーン中

2023/07/29 05:40

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -1,275 +1,138 @@
1
1
  なぜかこういう作りにする方が多いですよね(過去記事等のミスリードなんだと思います)
2
-
3
-
4
2
 
5
3
  この例だと依存関係プロパティを作る必要はありません。`DataContext`に`Texts`を渡すだけです。
6
4
 
7
-
8
-
9
5
  どうしてもというなら`RelativeSource`や`ElementName`で、`UserControl1`自身を引っ張ってくる必要があります。
10
-
11
-
12
6
 
13
7
  個人的にはユーザーコントロールで、依存関係プロパティが必要になることはほとんどないですね(カスタムコントロールでは多用しますが)
14
8
 
15
-
16
-
17
- ```xaml
9
+ ```xml:MainWindow.xaml
18
-
19
10
  <Window
20
-
21
11
  x:Class="Questions359857.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:Questions359857"
28
-
29
15
  Width="800"
30
-
31
16
  Height="450">
32
-
33
17
  <Window.Resources>
34
-
35
18
  <DataTemplate DataType="{x:Type local:Texts}">
36
-
37
19
  <local:UserControl2 />
38
-
39
20
  </DataTemplate>
40
-
41
21
  </Window.Resources>
42
-
43
22
  <Grid>
44
-
45
23
  <Grid.ColumnDefinitions>
46
-
47
24
  <ColumnDefinition />
48
-
49
25
  <ColumnDefinition />
50
-
51
26
  <ColumnDefinition />
52
-
53
27
  </Grid.ColumnDefinitions>
54
-
55
28
  <GroupBox Header="Original">
56
-
57
29
  <local:UserControl1 Value="{Binding ValueA.Value}" />
58
-
59
30
  </GroupBox>
60
-
61
31
  <GroupBox Grid.Column="1" Header="DataContext">
62
-
63
32
  <local:UserControl2 DataContext="{Binding ValueB.Value}" />
64
-
65
33
  </GroupBox>
66
-
67
34
  <GroupBox Grid.Column="2" Header="DataTemplate">
68
-
69
35
  <ContentControl Content="{Binding ValueC.Value}" />
70
-
71
36
  </GroupBox>
72
-
73
37
  </Grid>
74
-
75
38
  </Window>
76
-
77
39
  ```
78
40
 
79
-
80
-
81
- ```C#
41
+ ```cs
82
-
83
42
  using Reactive.Bindings;
84
-
85
43
  using System.Windows;
86
44
 
45
+ namespace Questions359857
46
+ {
47
+ public class Texts
48
+ {
49
+ public int A { get; set; } = 5;
50
+ public string B { get; set; } = "";
51
+ public double? C { get; set; }
52
+ }
53
+
54
+ public partial class MainWindow : Window
55
+ {
56
+ public ReactiveProperty<Texts> ValueA { get; } = new(new Texts { A = 1, B = "Ab", C = 10.0 });
57
+ public ReactiveProperty<Texts> ValueB { get; } = new(new Texts { A = 2, B = "Bb", C = 222.22 });
58
+ public ReactiveProperty<Texts> ValueC { get; } = new(new Texts { A = 3, B = "Cb", C = null });
59
+
60
+ public MainWindow()
61
+ {
62
+ InitializeComponent();
63
+ DataContext = this;
64
+ }
65
+ }
66
+ }
67
+ ```
68
+
69
+ ```xml:UserControl1.xaml
70
+ <UserControl
71
+ x:Class="Questions359857.UserControl1"
72
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
73
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
74
+ xmlns:local="clr-namespace:Questions359857"
75
+ x:Name="userControl1">
76
+ <StackPanel>
77
+ <HeaderedContentControl Header="A">
78
+ <TextBox Text="{Binding Value.A, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource AncestorType={x:Type local:UserControl1}}}" />
79
+ </HeaderedContentControl>
80
+ <HeaderedContentControl Header="B">
81
+ <TextBox Text="{Binding Value.B, UpdateSourceTrigger=PropertyChanged, ElementName=userControl1}" />
82
+ </HeaderedContentControl>
83
+ <HeaderedContentControl DataContext="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:UserControl1}}}" Header="C">
84
+ <TextBox Text="{Binding Value.C, UpdateSourceTrigger=PropertyChanged}" />
85
+ </HeaderedContentControl>
86
+ </StackPanel>
87
+ </UserControl>
88
+ ```
89
+
90
+ ```cs:UserControl1.xaml.cs
91
+ using System.Windows;
92
+ using System.Windows.Controls;
93
+
94
+ namespace Questions359857
95
+ {
96
+ public partial class UserControl1 : UserControl
97
+ {
98
+ public Texts Value { get => (Texts)GetValue(ValueProperty); set => SetValue(ValueProperty, value); }
99
+ public static readonly DependencyProperty ValueProperty
100
+ = DependencyProperty.Register(nameof(Value), typeof(Texts), typeof(UserControl1),
101
+ new PropertyMetadata(new Texts()));
102
+
103
+ public UserControl1() => InitializeComponent();
104
+ }
105
+ }
106
+ ```
107
+
108
+ ```xml:UserControl2.xaml
109
+ <UserControl
110
+ x:Class="Questions359857.UserControl2"
111
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
112
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
113
+ <StackPanel>
114
+ <HeaderedContentControl Header="A">
115
+ <TextBox Text="{Binding A, UpdateSourceTrigger=PropertyChanged}" />
116
+ </HeaderedContentControl>
117
+ <HeaderedContentControl Header="B">
118
+ <TextBox Text="{Binding B, UpdateSourceTrigger=PropertyChanged}" />
119
+ </HeaderedContentControl>
120
+ <HeaderedContentControl Header="C">
121
+ <TextBox Text="{Binding C, UpdateSourceTrigger=PropertyChanged}" />
122
+ </HeaderedContentControl>
123
+ </StackPanel>
124
+ </UserControl>
125
+ ```
126
+
127
+ ```cs:UserControl2.xaml.cs
128
+ using System.Windows.Controls;
87
129
 
88
130
 
89
131
  namespace Questions359857
90
-
91
132
  {
92
-
93
- public class Texts
133
+ public sealed partial class UserControl2 : UserControl
94
-
95
134
  {
96
-
97
- public int A { get; set; } = 5;
98
-
99
- public string B { get; set; } = "";
135
+ public UserControl2() => InitializeComponent();
100
-
101
- public double? C { get; set; }
102
-
103
136
  }
104
-
105
-
106
-
107
- public partial class MainWindow : Window
108
-
109
- {
110
-
111
- public ReactiveProperty<Texts> ValueA { get; } = new(new Texts { A = 1, B = "Ab", C = 10.0 });
112
-
113
- public ReactiveProperty<Texts> ValueB { get; } = new(new Texts { A = 2, B = "Bb", C = 222.22 });
114
-
115
- public ReactiveProperty<Texts> ValueC { get; } = new(new Texts { A = 3, B = "Cb", C = null });
116
-
117
-
118
-
119
- public MainWindow()
120
-
121
- {
122
-
123
- InitializeComponent();
124
-
125
- DataContext = this;
126
-
127
- }
128
-
129
- }
130
-
131
137
  }
132
-
133
138
  ```
134
-
135
-
136
-
137
- ```xaml
138
-
139
- <UserControl
140
-
141
- x:Class="Questions359857.UserControl1"
142
-
143
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
144
-
145
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
146
-
147
- xmlns:local="clr-namespace:Questions359857"
148
-
149
- x:Name="userControl1">
150
-
151
- <StackPanel>
152
-
153
- <HeaderedContentControl Header="A">
154
-
155
- <TextBox Text="{Binding Value.A, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource AncestorType={x:Type local:UserControl1}}}" />
156
-
157
- </HeaderedContentControl>
158
-
159
- <HeaderedContentControl Header="B">
160
-
161
- <TextBox Text="{Binding Value.B, UpdateSourceTrigger=PropertyChanged, ElementName=userControl1}" />
162
-
163
- </HeaderedContentControl>
164
-
165
- <HeaderedContentControl DataContext="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:UserControl1}}}" Header="C">
166
-
167
- <TextBox Text="{Binding Value.C, UpdateSourceTrigger=PropertyChanged}" />
168
-
169
- </HeaderedContentControl>
170
-
171
- </StackPanel>
172
-
173
- </UserControl>
174
-
175
- ```
176
-
177
-
178
-
179
- ```C#
180
-
181
- using System.Windows;
182
-
183
- using System.Windows.Controls;
184
-
185
-
186
-
187
- namespace Questions359857
188
-
189
- {
190
-
191
- public partial class UserControl1 : UserControl
192
-
193
- {
194
-
195
- public Texts Value { get => (Texts)GetValue(ValueProperty); set => SetValue(ValueProperty, value); }
196
-
197
- public static readonly DependencyProperty ValueProperty
198
-
199
- = DependencyProperty.Register(nameof(Value), typeof(Texts), typeof(UserControl1),
200
-
201
- new PropertyMetadata(new Texts()));
202
-
203
-
204
-
205
- public UserControl1() => InitializeComponent();
206
-
207
- }
208
-
209
- }
210
-
211
- ```
212
-
213
-
214
-
215
- ```xaml
216
-
217
- <UserControl
218
-
219
- x:Class="Questions359857.UserControl2"
220
-
221
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
222
-
223
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
224
-
225
- <StackPanel>
226
-
227
- <HeaderedContentControl Header="A">
228
-
229
- <TextBox Text="{Binding A, UpdateSourceTrigger=PropertyChanged}" />
230
-
231
- </HeaderedContentControl>
232
-
233
- <HeaderedContentControl Header="B">
234
-
235
- <TextBox Text="{Binding B, UpdateSourceTrigger=PropertyChanged}" />
236
-
237
- </HeaderedContentControl>
238
-
239
- <HeaderedContentControl Header="C">
240
-
241
- <TextBox Text="{Binding C, UpdateSourceTrigger=PropertyChanged}" />
242
-
243
- </HeaderedContentControl>
244
-
245
- </StackPanel>
246
-
247
- </UserControl>
248
-
249
- ```
250
-
251
-
252
-
253
- ```C#
254
-
255
- using System.Windows.Controls;
256
-
257
-
258
-
259
-
260
-
261
- namespace Questions359857
262
-
263
- {
264
-
265
- public sealed partial class UserControl2 : UserControl
266
-
267
- {
268
-
269
- public UserControl2() => InitializeComponent();
270
-
271
- }
272
-
273
- }
274
-
275
- ```