回答編集履歴
1
見直しキャンペーン中
answer
CHANGED
@@ -1,138 +1,138 @@
|
|
1
|
-
なぜかこういう作りにする方が多いですよね(過去記事等のミスリードなんだと思います)
|
2
|
-
|
3
|
-
この例だと依存関係プロパティを作る必要はありません。`DataContext`に`Texts`を渡すだけです。
|
4
|
-
|
5
|
-
どうしてもというなら`RelativeSource`や`ElementName`で、`UserControl1`自身を引っ張ってくる必要があります。
|
6
|
-
|
7
|
-
個人的にはユーザーコントロールで、依存関係プロパティが必要になることはほとんどないですね(カスタムコントロールでは多用しますが)
|
8
|
-
|
9
|
-
```xaml
|
10
|
-
<Window
|
11
|
-
x:Class="Questions359857.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:Questions359857"
|
15
|
-
Width="800"
|
16
|
-
Height="450">
|
17
|
-
<Window.Resources>
|
18
|
-
<DataTemplate DataType="{x:Type local:Texts}">
|
19
|
-
<local:UserControl2 />
|
20
|
-
</DataTemplate>
|
21
|
-
</Window.Resources>
|
22
|
-
<Grid>
|
23
|
-
<Grid.ColumnDefinitions>
|
24
|
-
<ColumnDefinition />
|
25
|
-
<ColumnDefinition />
|
26
|
-
<ColumnDefinition />
|
27
|
-
</Grid.ColumnDefinitions>
|
28
|
-
<GroupBox Header="Original">
|
29
|
-
<local:UserControl1 Value="{Binding ValueA.Value}" />
|
30
|
-
</GroupBox>
|
31
|
-
<GroupBox Grid.Column="1" Header="DataContext">
|
32
|
-
<local:UserControl2 DataContext="{Binding ValueB.Value}" />
|
33
|
-
</GroupBox>
|
34
|
-
<GroupBox Grid.Column="2" Header="DataTemplate">
|
35
|
-
<ContentControl Content="{Binding ValueC.Value}" />
|
36
|
-
</GroupBox>
|
37
|
-
</Grid>
|
38
|
-
</Window>
|
39
|
-
```
|
40
|
-
|
41
|
-
```
|
42
|
-
using Reactive.Bindings;
|
43
|
-
using System.Windows;
|
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
|
-
```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
|
-
```
|
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
|
-
```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
|
-
```
|
128
|
-
using System.Windows.Controls;
|
129
|
-
|
130
|
-
|
131
|
-
namespace Questions359857
|
132
|
-
{
|
133
|
-
public sealed partial class UserControl2 : UserControl
|
134
|
-
{
|
135
|
-
public UserControl2() => InitializeComponent();
|
136
|
-
}
|
137
|
-
}
|
1
|
+
なぜかこういう作りにする方が多いですよね(過去記事等のミスリードなんだと思います)
|
2
|
+
|
3
|
+
この例だと依存関係プロパティを作る必要はありません。`DataContext`に`Texts`を渡すだけです。
|
4
|
+
|
5
|
+
どうしてもというなら`RelativeSource`や`ElementName`で、`UserControl1`自身を引っ張ってくる必要があります。
|
6
|
+
|
7
|
+
個人的にはユーザーコントロールで、依存関係プロパティが必要になることはほとんどないですね(カスタムコントロールでは多用しますが)
|
8
|
+
|
9
|
+
```xml:MainWindow.xaml
|
10
|
+
<Window
|
11
|
+
x:Class="Questions359857.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:Questions359857"
|
15
|
+
Width="800"
|
16
|
+
Height="450">
|
17
|
+
<Window.Resources>
|
18
|
+
<DataTemplate DataType="{x:Type local:Texts}">
|
19
|
+
<local:UserControl2 />
|
20
|
+
</DataTemplate>
|
21
|
+
</Window.Resources>
|
22
|
+
<Grid>
|
23
|
+
<Grid.ColumnDefinitions>
|
24
|
+
<ColumnDefinition />
|
25
|
+
<ColumnDefinition />
|
26
|
+
<ColumnDefinition />
|
27
|
+
</Grid.ColumnDefinitions>
|
28
|
+
<GroupBox Header="Original">
|
29
|
+
<local:UserControl1 Value="{Binding ValueA.Value}" />
|
30
|
+
</GroupBox>
|
31
|
+
<GroupBox Grid.Column="1" Header="DataContext">
|
32
|
+
<local:UserControl2 DataContext="{Binding ValueB.Value}" />
|
33
|
+
</GroupBox>
|
34
|
+
<GroupBox Grid.Column="2" Header="DataTemplate">
|
35
|
+
<ContentControl Content="{Binding ValueC.Value}" />
|
36
|
+
</GroupBox>
|
37
|
+
</Grid>
|
38
|
+
</Window>
|
39
|
+
```
|
40
|
+
|
41
|
+
```cs
|
42
|
+
using Reactive.Bindings;
|
43
|
+
using System.Windows;
|
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;
|
129
|
+
|
130
|
+
|
131
|
+
namespace Questions359857
|
132
|
+
{
|
133
|
+
public sealed partial class UserControl2 : UserControl
|
134
|
+
{
|
135
|
+
public UserControl2() => InitializeComponent();
|
136
|
+
}
|
137
|
+
}
|
138
138
|
```
|