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

回答編集履歴

3

見直しキャンペーン中

2023/07/25 13:52

投稿

TN8001
TN8001

スコア10104

answer CHANGED
@@ -1,184 +1,176 @@
1
- おそらくダイアログ云々は関係なくて、`MainWindow`に置いても出ないんじゃないでしょうか?
2
- 現象から推測すると、
3
-
4
- * `XXX.xaml`で`AutoWireViewModel="True"`された上`XXXViewModel`がある
5
- つまり`DataContext`が違うのではないかということです。
6
- * `XXX.xaml`の`Param`を表示するコントロールで`RelativeSource`忘れ
7
- `DependencyProperty`の`Param`を中からバインドするなら、`FindAncestor`がいると思いますができていない?
8
-
9
- どちらにせよ「出力」ウィンドウや「XAML バインド エラー」ウィンドウに、エラー表示が出ます。
10
- エラーが出ていなければはずれですので、`XXX.xaml`・`XXX.xaml.cs`も追記してください。
11
-
12
-
13
- > DependencyProperty で実装しており、デバッガ で確認したところ、その setter も呼ばれていない
14
-
15
- CLRプロパティラッパーが呼ばれないのは仕様です。直接`SetValue`が呼ばれます。
16
-
17
- [XAML Loading and Dependency Properties - WPF .NET Framework | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/desktop/wpf/advanced/xaml-loading-and-dependency-properties)
18
-
19
- ---
20
-
21
-
22
- 検証コード
23
-
24
- Dialog.xaml
25
- ```xaml
26
- <UserControl
27
- x:Class="Questions309079.Views.Dialog"
28
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
29
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
30
- xmlns:local="clr-namespace:Questions309079.Views"
31
- xmlns:prism="http://prismlibrary.com/"
32
- x:Name="dialog"
33
- prism:ViewModelLocator.AutoWireViewModel="True">
34
- <StackPanel>
35
- <GroupBox Margin="5" Header="Dialog VM ⇆ XXX DP">
36
- <local:XXX
37
- x:Name="XXX"
38
- Param1="{Binding DataContext.Param1.Value, ElementName=dialog, Mode=TwoWay}"
39
- Param2="{Binding DataContext.Param2.Value, ElementName=dialog, Mode=TwoWay}" />
40
- </GroupBox>
41
-
42
- <GroupBox Margin="5" Header="XXX DP ⇆ XXX VM">
43
- <local:XXX
44
- Param1="{Binding Param1.Value, Mode=TwoWay}"
45
- Param2="{Binding Param2.Value, Mode=TwoWay}" />
46
- </GroupBox>
47
-
48
- <GroupBox Margin="5" Header="Dialog VM ⇆ XXX DP ⇆ XXX VM">
49
- <TextBlock Text="こういうことをしようとしている??&#xa;だったらXXX DPもXXX VMもいらんでしょ?という話" />
50
- </GroupBox>
51
-
52
- <GroupBox Margin="5" Header="Dialog">
53
- <StackPanel>
54
- <HeaderedContentControl Header="Dialog VM Param1">
55
- <TextBox Text="{Binding Param1.Value, UpdateSourceTrigger=PropertyChanged}" />
56
- </HeaderedContentControl>
57
- <HeaderedContentControl Header="Dialog VM Param2">
58
- <TextBox Text="{Binding Param2.Value, UpdateSourceTrigger=PropertyChanged}" />
59
- </HeaderedContentControl>
60
- </StackPanel>
61
- </GroupBox>
62
- </StackPanel>
63
- </UserControl>
64
- ```
65
-
66
- DialogViewModel.cs
67
- ```C#
68
- using Prism.Mvvm;
69
- using Prism.Services.Dialogs;
70
- using Reactive.Bindings;
71
- using System;
72
-
73
- namespace Questions309079.ViewModels
74
- {
75
- public class DialogViewModel : BindableBase, IDialogAware
76
- {
77
- public ReactiveProperty<string> Param1 { get; } = new ReactiveProperty<string>("aaa");
78
- public ReactiveProperty<string> Param2 { get; } = new ReactiveProperty<string>("bbb");
79
-
80
- public string Title => "Dialog";
81
-
82
- public event Action<IDialogResult> RequestClose;
83
-
84
- public void OnDialogOpened(IDialogParameters parameters)
85
- {
86
- Param1.Value = "ABC";
87
- Param2.Value = "DEF";
88
- }
89
-
90
- public void RaiseRequestClose(IDialogResult dialogResult) => RequestClose?.Invoke(dialogResult);
91
- public bool CanCloseDialog() => true;
92
- public void OnDialogClosed() { }
93
- }
94
- }
95
- ```
96
-
97
- ---
98
-
99
- XXX.xaml
100
- ```xaml
101
- <UserControl
102
- x:Class="Questions309079.Views.XXX"
103
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
104
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
105
- xmlns:local="clr-namespace:Questions309079.Views"
106
- xmlns:prism="http://prismlibrary.com/"
107
- prism:ViewModelLocator.AutoWireViewModel="True">
108
- <Grid>
109
- <Grid.ColumnDefinitions>
110
- <ColumnDefinition />
111
- <ColumnDefinition />
112
- </Grid.ColumnDefinitions>
113
- <StackPanel>
114
- <HeaderedContentControl Header="XXX DP Param1">
115
- <TextBox Text="{Binding Param1, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:XXX}}, UpdateSourceTrigger=PropertyChanged}" />
116
- </HeaderedContentControl>
117
- <HeaderedContentControl Header="XXX DP Param2">
118
- <TextBox Text="{Binding Param2, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:XXX}}, UpdateSourceTrigger=PropertyChanged}" />
119
- </HeaderedContentControl>
120
- </StackPanel>
121
-
122
- <StackPanel Grid.Column="1">
123
- <HeaderedContentControl Header="XXX VM Param1">
124
- <TextBox Text="{Binding Param1.Value, UpdateSourceTrigger=PropertyChanged}" />
125
- </HeaderedContentControl>
126
- <HeaderedContentControl Header="XXX VM Param2">
127
- <TextBox Text="{Binding Param2.Value, UpdateSourceTrigger=PropertyChanged}" />
128
- </HeaderedContentControl>
129
- </StackPanel>
130
- </Grid>
131
- </UserControl>
132
- ```
133
-
134
- XXX.xaml.cs
135
- ```C#
136
- using System.Diagnostics;
137
- using System.Windows;
138
- using System.Windows.Controls;
139
-
140
- namespace Questions309079.Views
141
- {
142
- public partial class XXX : UserControl
143
- {
144
- public string Param1
145
- {
146
- get => (string)GetValue(Param1Property);
147
- set
148
- {
149
- Debug.WriteLine($"setter:{Param1}->{value}");
150
- SetValue(Param1Property, value);
151
- }
152
- }
153
- public static readonly DependencyProperty Param1Property
154
- = DependencyProperty.Register(nameof(Param1), typeof(string), typeof(XXX),
155
- new PropertyMetadata(string.Empty, OnParam1Changed));
156
- private static void OnParam1Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
157
- => Debug.WriteLine($"OnParam1Changed:{e.OldValue}->{e.NewValue}");
158
-
159
-
160
- public string Param2 { get => (string)GetValue(Param2Property); set => SetValue(Param2Property, value); }
161
- public static readonly DependencyProperty Param2Property
162
- = DependencyProperty.Register(nameof(Param2), typeof(string), typeof(XXX),
163
- new PropertyMetadata(string.Empty));
164
-
165
- public XXX() => InitializeComponent();
166
- }
167
- }
168
- ```
169
-
170
- XXXViewModel.cs
171
- ```C#
172
- using Reactive.Bindings;
173
-
174
- namespace Questions309079.ViewModels
175
- {
176
- public class XXXViewModel
177
- {
178
- public ReactiveProperty<string> Param1 { get; } = new ReactiveProperty<string>("aaa");
179
- public ReactiveProperty<string> Param2 { get; } = new ReactiveProperty<string>("bbb");
180
- }
181
- }
182
- ```
183
-
1
+ おそらくダイアログ云々は関係なくて、`MainWindow`に置いても出ないんじゃないでしょうか?
2
+ 現象から推測すると、
3
+
4
+ * `XXX.xaml`で`AutoWireViewModel="True"`された上`XXXViewModel`がある
5
+ つまり`DataContext`が違うのではないかということです。
6
+ * `XXX.xaml`の`Param`を表示するコントロールで`RelativeSource`忘れ
7
+ `DependencyProperty`の`Param`を中からバインドするなら、`FindAncestor`がいると思いますができていない?
8
+
9
+ どちらにせよ「出力」ウィンドウや「XAML バインド エラー」ウィンドウに、エラー表示が出ます。
10
+ エラーが出ていなければはずれですので、`XXX.xaml`・`XXX.xaml.cs`も追記してください。
11
+
12
+
13
+ > DependencyProperty で実装しており、デバッガ で確認したところ、その setter も呼ばれていない
14
+
15
+ CLRプロパティラッパーが呼ばれないのは仕様です。直接`SetValue`が呼ばれます。
16
+ [XAML Loading and Dependency Properties - WPF .NET Framework | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/desktop/wpf/advanced/xaml-loading-and-dependency-properties)
17
+
18
+ ---
19
+
20
+ 検証コード
21
+ ```xml:Dialog.xaml
22
+ <UserControl
23
+ x:Class="Questions309079.Views.Dialog"
24
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
25
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
26
+ xmlns:local="clr-namespace:Questions309079.Views"
27
+ xmlns:prism="http://prismlibrary.com/"
28
+ x:Name="dialog"
29
+ prism:ViewModelLocator.AutoWireViewModel="True">
30
+ <StackPanel>
31
+ <GroupBox Margin="5" Header="Dialog VM ⇆ XXX DP">
32
+ <local:XXX
33
+ x:Name="XXX"
34
+ Param1="{Binding DataContext.Param1.Value, ElementName=dialog, Mode=TwoWay}"
35
+ Param2="{Binding DataContext.Param2.Value, ElementName=dialog, Mode=TwoWay}" />
36
+ </GroupBox>
37
+
38
+ <GroupBox Margin="5" Header="XXX DP ⇆ XXX VM">
39
+ <local:XXX
40
+ Param1="{Binding Param1.Value, Mode=TwoWay}"
41
+ Param2="{Binding Param2.Value, Mode=TwoWay}" />
42
+ </GroupBox>
43
+
44
+ <GroupBox Margin="5" Header="Dialog VM ⇆ XXX DP ⇆ XXX VM">
45
+ <TextBlock Text="こういうことをしようとしている??&#xa;だったらXXX DPもXXX VMもいらんでしょ?という話" />
46
+ </GroupBox>
47
+
48
+ <GroupBox Margin="5" Header="Dialog">
49
+ <StackPanel>
50
+ <HeaderedContentControl Header="Dialog VM Param1">
51
+ <TextBox Text="{Binding Param1.Value, UpdateSourceTrigger=PropertyChanged}" />
52
+ </HeaderedContentControl>
53
+ <HeaderedContentControl Header="Dialog VM Param2">
54
+ <TextBox Text="{Binding Param2.Value, UpdateSourceTrigger=PropertyChanged}" />
55
+ </HeaderedContentControl>
56
+ </StackPanel>
57
+ </GroupBox>
58
+ </StackPanel>
59
+ </UserControl>
60
+ ```
61
+
62
+ ```cs:DialogViewModel.cs
63
+ using Prism.Mvvm;
64
+ using Prism.Services.Dialogs;
65
+ using Reactive.Bindings;
66
+ using System;
67
+
68
+ namespace Questions309079.ViewModels
69
+ {
70
+ public class DialogViewModel : BindableBase, IDialogAware
71
+ {
72
+ public ReactiveProperty<string> Param1 { get; } = new ReactiveProperty<string>("aaa");
73
+ public ReactiveProperty<string> Param2 { get; } = new ReactiveProperty<string>("bbb");
74
+
75
+ public string Title => "Dialog";
76
+
77
+ public event Action<IDialogResult> RequestClose;
78
+
79
+ public void OnDialogOpened(IDialogParameters parameters)
80
+ {
81
+ Param1.Value = "ABC";
82
+ Param2.Value = "DEF";
83
+ }
84
+
85
+ public void RaiseRequestClose(IDialogResult dialogResult) => RequestClose?.Invoke(dialogResult);
86
+ public bool CanCloseDialog() => true;
87
+ public void OnDialogClosed() { }
88
+ }
89
+ }
90
+ ```
91
+
92
+ ---
93
+
94
+ ```xml:XXX.xaml
95
+ <UserControl
96
+ x:Class="Questions309079.Views.XXX"
97
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
98
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
99
+ xmlns:local="clr-namespace:Questions309079.Views"
100
+ xmlns:prism="http://prismlibrary.com/"
101
+ prism:ViewModelLocator.AutoWireViewModel="True">
102
+ <Grid>
103
+ <Grid.ColumnDefinitions>
104
+ <ColumnDefinition />
105
+ <ColumnDefinition />
106
+ </Grid.ColumnDefinitions>
107
+ <StackPanel>
108
+ <HeaderedContentControl Header="XXX DP Param1">
109
+ <TextBox Text="{Binding Param1, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:XXX}}, UpdateSourceTrigger=PropertyChanged}" />
110
+ </HeaderedContentControl>
111
+ <HeaderedContentControl Header="XXX DP Param2">
112
+ <TextBox Text="{Binding Param2, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:XXX}}, UpdateSourceTrigger=PropertyChanged}" />
113
+ </HeaderedContentControl>
114
+ </StackPanel>
115
+
116
+ <StackPanel Grid.Column="1">
117
+ <HeaderedContentControl Header="XXX VM Param1">
118
+ <TextBox Text="{Binding Param1.Value, UpdateSourceTrigger=PropertyChanged}" />
119
+ </HeaderedContentControl>
120
+ <HeaderedContentControl Header="XXX VM Param2">
121
+ <TextBox Text="{Binding Param2.Value, UpdateSourceTrigger=PropertyChanged}" />
122
+ </HeaderedContentControl>
123
+ </StackPanel>
124
+ </Grid>
125
+ </UserControl>
126
+ ```
127
+
128
+ ```cs:XXX.xaml.cs
129
+ using System.Diagnostics;
130
+ using System.Windows;
131
+ using System.Windows.Controls;
132
+
133
+ namespace Questions309079.Views
134
+ {
135
+ public partial class XXX : UserControl
136
+ {
137
+ public string Param1
138
+ {
139
+ get => (string)GetValue(Param1Property);
140
+ set
141
+ {
142
+ Debug.WriteLine($"setter:{Param1}->{value}");
143
+ SetValue(Param1Property, value);
144
+ }
145
+ }
146
+ public static readonly DependencyProperty Param1Property
147
+ = DependencyProperty.Register(nameof(Param1), typeof(string), typeof(XXX),
148
+ new PropertyMetadata(string.Empty, OnParam1Changed));
149
+ private static void OnParam1Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
150
+ => Debug.WriteLine($"OnParam1Changed:{e.OldValue}->{e.NewValue}");
151
+
152
+
153
+ public string Param2 { get => (string)GetValue(Param2Property); set => SetValue(Param2Property, value); }
154
+ public static readonly DependencyProperty Param2Property
155
+ = DependencyProperty.Register(nameof(Param2), typeof(string), typeof(XXX),
156
+ new PropertyMetadata(string.Empty));
157
+
158
+ public XXX() => InitializeComponent();
159
+ }
160
+ }
161
+ ```
162
+
163
+ ```cs:XXXViewModel.cs
164
+ using Reactive.Bindings;
165
+
166
+ namespace Questions309079.ViewModels
167
+ {
168
+ public class XXXViewModel
169
+ {
170
+ public ReactiveProperty<string> Param1 { get; } = new ReactiveProperty<string>("aaa");
171
+ public ReactiveProperty<string> Param2 { get; } = new ReactiveProperty<string>("bbb");
172
+ }
173
+ }
174
+ ```
175
+
184
176
  ![アプリ画像](cc96a7a1d584cf8c258b9ec28dbc10f0.png)

2

(さらにわかりやすく)更新しました。

2020/12/14 04:25

投稿

TN8001
TN8001

スコア10104

answer CHANGED
@@ -18,10 +18,10 @@
18
18
 
19
19
  ---
20
20
 
21
- ### XXXViewModel は必要
22
21
 
23
- `XXX`作成時に`XXXViewModel`が`DataContext`に差し込まれますから、`Param.Value`は他から取ってくることになります。
22
+ 検証コード
24
23
 
24
+ Dialog.xaml
25
25
  ```xaml
26
26
  <UserControl
27
27
  x:Class="Questions309079.Views.Dialog"
@@ -32,12 +32,71 @@
32
32
  x:Name="dialog"
33
33
  prism:ViewModelLocator.AutoWireViewModel="True">
34
34
  <StackPanel>
35
+ <GroupBox Margin="5" Header="Dialog VM ⇆ XXX DP">
36
+ <local:XXX
37
+ x:Name="XXX"
38
+ Param1="{Binding DataContext.Param1.Value, ElementName=dialog, Mode=TwoWay}"
35
- <local:XXX x:Name="XXX" Param="{Binding DataContext.Param.Value, ElementName=dialog, Mode=TwoWay}" />
39
+ Param2="{Binding DataContext.Param2.Value, ElementName=dialog, Mode=TwoWay}" />
40
+ </GroupBox>
41
+
42
+ <GroupBox Margin="5" Header="XXX DP ⇆ XXX VM">
43
+ <local:XXX
44
+ Param1="{Binding Param1.Value, Mode=TwoWay}"
45
+ Param2="{Binding Param2.Value, Mode=TwoWay}" />
46
+ </GroupBox>
47
+
48
+ <GroupBox Margin="5" Header="Dialog VM ⇆ XXX DP ⇆ XXX VM">
49
+ <TextBlock Text="こういうことをしようとしている??&#xa;だったらXXX DPもXXX VMもいらんでしょ?という話" />
50
+ </GroupBox>
51
+
52
+ <GroupBox Margin="5" Header="Dialog">
53
+ <StackPanel>
54
+ <HeaderedContentControl Header="Dialog VM Param1">
36
- <TextBox Text="{Binding Param.Value, UpdateSourceTrigger=PropertyChanged}" />
55
+ <TextBox Text="{Binding Param1.Value, UpdateSourceTrigger=PropertyChanged}" />
56
+ </HeaderedContentControl>
57
+ <HeaderedContentControl Header="Dialog VM Param2">
58
+ <TextBox Text="{Binding Param2.Value, UpdateSourceTrigger=PropertyChanged}" />
59
+ </HeaderedContentControl>
60
+ </StackPanel>
61
+ </GroupBox>
37
62
  </StackPanel>
38
63
  </UserControl>
39
64
  ```
40
65
 
66
+ DialogViewModel.cs
67
+ ```C#
68
+ using Prism.Mvvm;
69
+ using Prism.Services.Dialogs;
70
+ using Reactive.Bindings;
71
+ using System;
72
+
73
+ namespace Questions309079.ViewModels
74
+ {
75
+ public class DialogViewModel : BindableBase, IDialogAware
76
+ {
77
+ public ReactiveProperty<string> Param1 { get; } = new ReactiveProperty<string>("aaa");
78
+ public ReactiveProperty<string> Param2 { get; } = new ReactiveProperty<string>("bbb");
79
+
80
+ public string Title => "Dialog";
81
+
82
+ public event Action<IDialogResult> RequestClose;
83
+
84
+ public void OnDialogOpened(IDialogParameters parameters)
85
+ {
86
+ Param1.Value = "ABC";
87
+ Param2.Value = "DEF";
88
+ }
89
+
90
+ public void RaiseRequestClose(IDialogResult dialogResult) => RequestClose?.Invoke(dialogResult);
91
+ public bool CanCloseDialog() => true;
92
+ public void OnDialogClosed() { }
93
+ }
94
+ }
95
+ ```
96
+
97
+ ---
98
+
99
+ XXX.xaml
41
100
  ```xaml
42
101
  <UserControl
43
102
  x:Class="Questions309079.Views.XXX"
@@ -46,43 +105,80 @@
46
105
  xmlns:local="clr-namespace:Questions309079.Views"
47
106
  xmlns:prism="http://prismlibrary.com/"
48
107
  prism:ViewModelLocator.AutoWireViewModel="True">
108
+ <Grid>
109
+ <Grid.ColumnDefinitions>
110
+ <ColumnDefinition />
111
+ <ColumnDefinition />
112
+ </Grid.ColumnDefinitions>
49
- <StackPanel>
113
+ <StackPanel>
50
- <HeaderedContentControl Header="Param">
114
+ <HeaderedContentControl Header="XXX DP Param1">
51
- <TextBox Text="{Binding Param, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:XXX}}, UpdateSourceTrigger=PropertyChanged}" />
115
+ <TextBox Text="{Binding Param1, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:XXX}}, UpdateSourceTrigger=PropertyChanged}" />
52
- </HeaderedContentControl>
116
+ </HeaderedContentControl>
117
+ <HeaderedContentControl Header="XXX DP Param2">
118
+ <TextBox Text="{Binding Param2, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:XXX}}, UpdateSourceTrigger=PropertyChanged}" />
119
+ </HeaderedContentControl>
53
- </StackPanel>
120
+ </StackPanel>
121
+
122
+ <StackPanel Grid.Column="1">
123
+ <HeaderedContentControl Header="XXX VM Param1">
124
+ <TextBox Text="{Binding Param1.Value, UpdateSourceTrigger=PropertyChanged}" />
125
+ </HeaderedContentControl>
126
+ <HeaderedContentControl Header="XXX VM Param2">
127
+ <TextBox Text="{Binding Param2.Value, UpdateSourceTrigger=PropertyChanged}" />
128
+ </HeaderedContentControl>
129
+ </StackPanel>
130
+ </Grid>
54
131
  </UserControl>
55
132
  ```
56
133
 
134
+ XXX.xaml.cs
135
+ ```C#
57
- ### XXXViewModel はなくてもいい
136
+ using System.Diagnostics;
137
+ using System.Windows;
58
- `XXXViewModel`はカラか、`DialogViewModel`に移しても問題ない場合。
138
+ using System.Windows.Controls;
59
139
 
140
+ namespace Questions309079.Views
141
+ {
142
+ public partial class XXX : UserControl
143
+ {
144
+ public string Param1
145
+ {
146
+ get => (string)GetValue(Param1Property);
147
+ set
148
+ {
149
+ Debug.WriteLine($"setter:{Param1}->{value}");
150
+ SetValue(Param1Property, value);
151
+ }
152
+ }
153
+ public static readonly DependencyProperty Param1Property
154
+ = DependencyProperty.Register(nameof(Param1), typeof(string), typeof(XXX),
155
+ new PropertyMetadata(string.Empty, OnParam1Changed));
60
- `XXX`で`AutoWireViewModel="False"`すればいいですが、そもそも`DependencyProperty`がいらなくなってしまいますね^^;
156
+ private static void OnParam1Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
157
+ => Debug.WriteLine($"OnParam1Changed:{e.OldValue}->{e.NewValue}");
61
158
 
62
- ```xaml
159
+
63
- <UserControl
64
- x:Class="Questions309079.Views.Dialog"
65
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
160
+ public string Param2 { get => (string)GetValue(Param2Property); set => SetValue(Param2Property, value); }
66
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
67
- xmlns:local="clr-namespace:Questions309079.Views"
68
- xmlns:prism="http://prismlibrary.com/"
69
- prism:ViewModelLocator.AutoWireViewModel="True">
161
+ public static readonly DependencyProperty Param2Property
162
+ = DependencyProperty.Register(nameof(Param2), typeof(string), typeof(XXX),
70
- <StackPanel>
163
+ new PropertyMetadata(string.Empty));
164
+
71
- <local:XXX x:Name="XXX" />
165
+ public XXX() => InitializeComponent();
72
- <TextBox Text="{Binding Param.Value, UpdateSourceTrigger=PropertyChanged}" />
73
- </StackPanel>
166
+ }
74
- </UserControl>
167
+ }
75
168
  ```
76
169
 
170
+ XXXViewModel.cs
77
- ```xaml
171
+ ```C#
78
- <UserControl
172
+ using Reactive.Bindings;
173
+
79
- x:Class="Questions309079.Views.XXX"
174
+ namespace Questions309079.ViewModels
80
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
81
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
175
+ {
82
- <StackPanel>
176
+ public class XXXViewModel
83
- <HeaderedContentControl Header="Param">
177
+ {
84
- <TextBox Text="{Binding Param.Value, UpdateSourceTrigger=PropertyChanged}" />
178
+ public ReactiveProperty<string> Param1 { get; } = new ReactiveProperty<string>("aaa");
85
- </HeaderedContentControl>
179
+ public ReactiveProperty<string> Param2 { get; } = new ReactiveProperty<string>("bbb");
86
- </StackPanel>
180
+ }
87
- </UserControl>
181
+ }
88
- ```
182
+ ```
183
+
184
+ ![アプリ画像](cc96a7a1d584cf8c258b9ec28dbc10f0.png)

1

xaml追加

2020/12/14 04:25

投稿

TN8001
TN8001

スコア10104

answer CHANGED
@@ -14,4 +14,75 @@
14
14
 
15
15
  CLRプロパティラッパーが呼ばれないのは仕様です。直接`SetValue`が呼ばれます。
16
16
 
17
- [XAML Loading and Dependency Properties - WPF .NET Framework | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/desktop/wpf/advanced/xaml-loading-and-dependency-properties)
17
+ [XAML Loading and Dependency Properties - WPF .NET Framework | Microsoft Docs](https://docs.microsoft.com/ja-jp/dotnet/desktop/wpf/advanced/xaml-loading-and-dependency-properties)
18
+
19
+ ---
20
+
21
+ ### XXXViewModel は必要
22
+
23
+ `XXX`作成時に`XXXViewModel`が`DataContext`に差し込まれますから、`Param.Value`は他から取ってくることになります。
24
+
25
+ ```xaml
26
+ <UserControl
27
+ x:Class="Questions309079.Views.Dialog"
28
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
29
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
30
+ xmlns:local="clr-namespace:Questions309079.Views"
31
+ xmlns:prism="http://prismlibrary.com/"
32
+ x:Name="dialog"
33
+ prism:ViewModelLocator.AutoWireViewModel="True">
34
+ <StackPanel>
35
+ <local:XXX x:Name="XXX" Param="{Binding DataContext.Param.Value, ElementName=dialog, Mode=TwoWay}" />
36
+ <TextBox Text="{Binding Param.Value, UpdateSourceTrigger=PropertyChanged}" />
37
+ </StackPanel>
38
+ </UserControl>
39
+ ```
40
+
41
+ ```xaml
42
+ <UserControl
43
+ x:Class="Questions309079.Views.XXX"
44
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
45
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
46
+ xmlns:local="clr-namespace:Questions309079.Views"
47
+ xmlns:prism="http://prismlibrary.com/"
48
+ prism:ViewModelLocator.AutoWireViewModel="True">
49
+ <StackPanel>
50
+ <HeaderedContentControl Header="Param">
51
+ <TextBox Text="{Binding Param, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:XXX}}, UpdateSourceTrigger=PropertyChanged}" />
52
+ </HeaderedContentControl>
53
+ </StackPanel>
54
+ </UserControl>
55
+ ```
56
+
57
+ ### XXXViewModel はなくてもいい
58
+ `XXXViewModel`はカラか、`DialogViewModel`に移しても問題ない場合。
59
+
60
+ `XXX`で`AutoWireViewModel="False"`すればいいですが、そもそも`DependencyProperty`がいらなくなってしまいますね^^;
61
+
62
+ ```xaml
63
+ <UserControl
64
+ x:Class="Questions309079.Views.Dialog"
65
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
66
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
67
+ xmlns:local="clr-namespace:Questions309079.Views"
68
+ xmlns:prism="http://prismlibrary.com/"
69
+ prism:ViewModelLocator.AutoWireViewModel="True">
70
+ <StackPanel>
71
+ <local:XXX x:Name="XXX" />
72
+ <TextBox Text="{Binding Param.Value, UpdateSourceTrigger=PropertyChanged}" />
73
+ </StackPanel>
74
+ </UserControl>
75
+ ```
76
+
77
+ ```xaml
78
+ <UserControl
79
+ x:Class="Questions309079.Views.XXX"
80
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
81
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
82
+ <StackPanel>
83
+ <HeaderedContentControl Header="Param">
84
+ <TextBox Text="{Binding Param.Value, UpdateSourceTrigger=PropertyChanged}" />
85
+ </HeaderedContentControl>
86
+ </StackPanel>
87
+ </UserControl>
88
+ ```