質問編集履歴

1

追記です。

2020/09/19 14:43

投稿

Zee
Zee

スコア18

test CHANGED
File without changes
test CHANGED
@@ -1,13 +1,161 @@
1
1
  WPFにてUserControlをV, VM, Model の3階層で作成しています。親WindowのVMからこのUserControlに初期値を与え、終了時に結果を取得をしたいです。
2
2
 
3
- そのデータは、UserControlのModel部のデータ形式 (List<string>) です。
3
+ そのデータは、UserControlのModel部のデータ形式 (List<Info>) です。
4
+
5
+ Infoクラスにはstringやboolなどのメンバーがあります。
4
6
 
5
7
 
6
8
 
7
- 親ウインドウからどのようにUserControlに双方向のデータの受け渡しをしたらよいしょうか。MVVM的にこんな感じにできるといいので
9
+ 追記です。
8
-
9
- <MyUserControl Setting = "{Binding Path=Setting, Mode=TwoWay}"/>
10
10
 
11
11
 
12
12
 
13
+ UserControlにて依存プロパティにより 親Windowからは
14
+
15
+ <TestControl InfoList="{Binding Path=InfoList, Mode=TwoWay}"/>
16
+
17
+ の形でバインドが出来ました。
18
+
19
+
20
+
21
+ **一方でUserControl内は、依存プロパティの実装箇所からvmと直接やり取りをしています。
22
+
13
- は、他の方法で良い方法があれば教えてください
23
+ この個所をバインドを経由し伝達方法はあるしょうか
24
+
25
+ **
26
+
27
+ 以下、usercontrolのxamlとコードビハインド部分です。
28
+
29
+
30
+
31
+ usercontrol xaml
32
+
33
+ ```c#
34
+
35
+ <UserControl x:Class="Test.TestControl"
36
+
37
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
38
+
39
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
40
+
41
+ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
42
+
43
+ xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
44
+
45
+ xmlns:zzz="clr-namespace:Test"
46
+
47
+ mc:Ignorable="d"
48
+
49
+ d:DesignHeight="400" Width="300"
50
+
51
+ >
52
+
53
+ <Grid Name="baseContainer">
54
+
55
+ <TreeView x:Name="TreeView" ItemsSource="{Binding Path=List}">
56
+
57
+ <TreeView.Resources>
58
+
59
+ <Style TargetType="TreeViewItem">
60
+
61
+ <Setter Property="IsExpanded" Value="{Binding Path=Expand,Mode=TwoWay}"/>
62
+
63
+ </Style>
64
+
65
+ </TreeView.Resources>
66
+
67
+
68
+
69
+ <TreeView.ItemTemplate>
70
+
71
+ <HierarchicalDataTemplate DataType= "zzz:List" ItemsSource="{Binding Path=Child}">
72
+
73
+ <StackPanel Orientation="Horizontal">
74
+
75
+ <CheckBox IsChecked="{Binding Path=Check, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
76
+
77
+ <Image Source="{Binding Path=Icon}"/>
78
+
79
+ <TextBlock Text="{Binding Path=Name}"/>
80
+
81
+ </StackPanel>
82
+
83
+ </HierarchicalDataTemplate>
84
+
85
+ </TreeView.ItemTemplate>
86
+
87
+ </TreeView>
88
+
89
+ </Grid>
90
+
91
+ </UserControl>
92
+
93
+ ```
94
+
95
+
96
+
97
+ ```c#
98
+
99
+ namespace Test
100
+
101
+ {
102
+
103
+ public partial class TestControl : UserControl
104
+
105
+ {
106
+
107
+ public TestControl()
108
+
109
+ {
110
+
111
+ InitializeComponent();
112
+
113
+ baseContainer.DataContext = new MainViewModel(this); // vmにvを渡す
114
+
115
+ }
116
+
117
+
118
+
119
+ public static readonly DependencyProperty InfoListProperty = DependencyProperty.Register(
120
+
121
+ "InfoList", // プロパティ名を指定
122
+
123
+ typeof(List<Info>), // プロパティの型を指定
124
+
125
+ typeof(TestControl), // プロパティを所有する型を指定
126
+
127
+ new PropertyMetadata(null, MyPropertyChangedHandler)
128
+
129
+ );
130
+
131
+
132
+
133
+ private static void MyPropertyChangedHandler(DependencyObject sender, DependencyPropertyChangedEventArgs args)
134
+
135
+ {
136
+
137
+ TestControl that = (TestControl)sender;
138
+
139
+ MainViewModel vm = (MainViewModel)that.baseContainer.DataContext;
140
+
141
+ vm.ListUpdate((List<Info>)args.NewValue); // v->vmへの通知
142
+
143
+ }
144
+
145
+
146
+
147
+ public List<Info> InfoList
148
+
149
+ {
150
+
151
+ get { return (List<Info>)GetValue(InfoListProperty); }
152
+
153
+ set { SetValue(InfoListProperty, value); } // vm->vへの通知
154
+
155
+ }
156
+
157
+ }
158
+
159
+ }
160
+
161
+ ```