回答編集履歴

1

xaml全文を受けての修正

2018/05/29 07:34

投稿

ponpu1601
ponpu1601

スコア166

test CHANGED
@@ -1,23 +1,143 @@
1
- できればxamlの全文を見せていただきたいところですが難しいでしょうか?
1
+ ~~できればxamlの全文を見せていただきたいところですが難しいでしょうか?~~
2
+
3
+ ご対応ありがとうございます。
2
4
 
3
5
 
4
6
 
5
- ただ、TextBoxでそのような形でBindingできているのであれば、
7
+ ~~ただ、TextBoxでそのような形でBindingできているのであれば、~~
6
8
 
7
- ComboBoxでSelectedValueにCategoryIDをBindingすることで実現可能なような気がします。
9
+ ~~ComboBoxでSelectedValueにCategoryIDをBindingすることで実現可能なような気がします。~~
8
10
 
9
- 下記のサンプルを試していただければと思います。
11
+ ~~下記のサンプルを試していただければと思います。~~
12
+
13
+
14
+
15
+ CollectionViewSourceの動作に自信がありませんが、下記のようにDataContextにpersonViewSourceが
16
+
17
+ 設定されているObject下にComboBoxを置く必要があるかと思います。
18
+
19
+ 一応手元の環境でpersonViewSourceまで選択したCategoryIDが反映されるところまでは動作確認しました。
10
20
 
11
21
 
12
22
 
13
23
  ```
14
24
 
15
- <ComboBox x:Name="comboTest" HorizontalAlignment="Left" Margin="168,55,0,0" VerticalAlignment="Top" Width="120"
25
+ <Window x:Class="ComboTest.MainWindow"
16
26
 
17
- SelectedValue="{Binding CategoryID,UpdateSourceTrigger=PropertyChanged}"
27
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
18
28
 
19
- />
29
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
30
+
31
+ xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
32
+
33
+ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
34
+
35
+ xmlns:local="clr-namespace:ComboTest"
36
+
37
+ mc:Ignorable="d"
38
+
39
+ Title="MainWindow" Height="450" Width="572.14" Loaded="Window_Loaded">
40
+
41
+ <Window.Resources>
42
+
43
+ <CollectionViewSource x:Key="personViewSource" d:DesignSource="{d:DesignInstance {x:Type local:Person}, CreateList=True}"/>
44
+
45
+ </Window.Resources>
46
+
47
+ <!--ここに参照を追加-->
48
+
49
+ <Grid DataContext="{StaticResource personViewSource}">
50
+
51
+ <Grid x:Name="grid1" DataContext="{StaticResource personViewSource}" HorizontalAlignment="Left" Margin="302,96,0,0" VerticalAlignment="Top">
52
+
53
+ <Grid.ColumnDefinitions>
54
+
55
+ <ColumnDefinition Width="Auto"/>
56
+
57
+ <ColumnDefinition/>
58
+
59
+ </Grid.ColumnDefinitions>
60
+
61
+ <Grid.RowDefinitions>
62
+
63
+ <RowDefinition Height="Auto"/>
64
+
65
+ </Grid.RowDefinitions>
66
+
67
+ </Grid>
68
+
69
+ <Grid x:Name="grid2" DataContext="{StaticResource personViewSource}" HorizontalAlignment="Left" Margin="127,176,0,0" VerticalAlignment="Top">
70
+
71
+ <Grid.ColumnDefinitions>
72
+
73
+ <ColumnDefinition Width="Auto"/>
74
+
75
+ <ColumnDefinition Width="Auto"/>
76
+
77
+ </Grid.ColumnDefinitions>
78
+
79
+ <Grid.RowDefinitions>
80
+
81
+ <RowDefinition Height="Auto"/>
82
+
83
+ </Grid.RowDefinitions>
84
+
85
+ <Label Content="名前:" Grid.Column="0" HorizontalAlignment="Left" Margin="3" Grid.Row="0" VerticalAlignment="Center"/>
86
+
87
+ <TextBox x:Name="名前TextBox" Grid.Column="1" HorizontalAlignment="Left" Height="23" Margin="3" Grid.Row="0" Text="{Binding 名前, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" VerticalAlignment="Center" Width="120"/>
88
+
89
+ </Grid>
90
+
91
+ <ComboBox x:Name="comboTest" HorizontalAlignment="Left" Margin="168,55,0,0" VerticalAlignment="Top" Width="120"
92
+
93
+ SelectedValue={Binding CategoryID,UpdateSourceTrigger=PropertyChanged}/>
94
+
95
+ <Grid x:Name="grid3" DataContext="{StaticResource personViewSource}" HorizontalAlignment="Left" Margin="86,140,0,0" VerticalAlignment="Top">
96
+
97
+ <Grid.ColumnDefinitions>
98
+
99
+ <ColumnDefinition Width="Auto"/>
100
+
101
+ <ColumnDefinition Width="Auto"/>
102
+
103
+ </Grid.ColumnDefinitions>
104
+
105
+ <Grid.RowDefinitions>
106
+
107
+ <RowDefinition Height="Auto"/>
108
+
109
+ </Grid.RowDefinitions>
110
+
111
+ <Label Content="Category ID:" Grid.Column="0" HorizontalAlignment="Left" Margin="3" Grid.Row="0" VerticalAlignment="Center"/>
112
+
113
+ <TextBox x:Name="categoryIDTextBox" Grid.Column="1" HorizontalAlignment="Left" Height="23" Margin="3" Grid.Row="0" Text="{Binding CategoryID, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" VerticalAlignment="Center" Width="120"/>
114
+
115
+ </Grid>
116
+
117
+ <Button Content="登録" HorizontalAlignment="Left" Margin="174,231,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
118
+
119
+ <TextBlock HorizontalAlignment="Left" Margin="298,144,0,0" TextWrapping="Wrap" Text="バインドされている。" VerticalAlignment="Top" Height="23" Width="117"/>
120
+
121
+ <TextBlock HorizontalAlignment="Left" Margin="302,56,0,0" TextWrapping="Wrap" Text="バインドされていない。" VerticalAlignment="Top" Height="23" Width="122"/>
122
+
123
+
124
+
125
+ </Grid>
126
+
127
+ </Window>
20
128
 
21
129
 
22
130
 
23
131
  ```
132
+
133
+
134
+
135
+
136
+
137
+ ※念のための確認
138
+
139
+ このぐらいの操作であればCollectionViewSourceは不要で、
140
+
141
+ 単にWindowのDataContextにPersonを設定すれば動作するように思えますが、
142
+
143
+ ColectionViewSourceを使用している理由はございますか?