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

質問編集履歴

1

追記です。

2020/09/19 14:43

投稿

Zee
Zee

スコア18

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