データグリッド内で操作するときにコンボボックス選択値を保存するのに苦労しました。私がデータグリッドを持たないテストソリューションを作ったら、うまくいきました。コンテキストとは、関連する国の人名です。国はxmlファイルに格納されます。MVVM WPFコンボボックスSelectedItemバインドがデータグリッド内でアクティブ化されない
はあなたがここにPersonList.xaml(の重要な部分)を参照してください:
<UserControl.Resources>
<XmlDataProvider x:Key="Dataxml" Source="\Properties\AllCountries.xml" />
<model:Person x:Key="Person"/>
</UserControl.Resources>
<UserControl.DataContext>
<viewModel:PersonListViewModel />
</UserControl.DataContext>
<DataGrid ItemsSource="{Binding Persons}" AutoGenerateColumns="False" IsReadOnly="False" CanUserAddRows="False" SelectionUnit="FullRow" SelectedItem="{Binding SelectedPerson}" >
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" CanUserSort="true" ></DataGridTextColumn>
<DataGridTemplateColumn Header="Country">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox
Width="150"
SelectedValuePath="country"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding Source={StaticResource Dataxml}, XPath=/countries/country}"
SelectedIndex="{Binding CountryIndex}"
SelectedItem="{Binding Path=XmlCountry, Mode=TwoWay}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0}, {1}">
<Binding XPath="name" />
<Binding XPath="iso" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
このグリッドはPersonListViewModelから移入さを持っていること、ここで最初のビューのスナップショットがありますプライベートObservableCollection<Person> _persons
属性は、INotifyPropertyChangedを実装し、グリッドのItemsSourceです。また、グリッドにSelectedItem="{Binding SelectedPerson}"
が表示されます。その部分はうまくいく。
PersonモデルクラスにはCountryIndex(文字列、xmlファイルのインデックス、計算値)、国(文字列、国名)があり、XmlCountry属性(XmlElement、xmlファイルのxmlnode)が実装されました。私は人の国の名前を使用すると、スクリーンショットで見るように初期値を設定するために使用されている国の指標を計算するために使用されるのViewModelコンストラクタで人物をロードすると
?xml version="1.0" encoding="utf-8"?>
<countries>
<country>
<iso>AF</iso>
<name>Afghanistan</name>
</country>
<country>
<iso>AL</iso>
<name>Albania</name>
</country>
<country>
<iso>DZ</iso>
<name>Algeria</name>
</country>
<country>
<iso>AS</iso>
<name>American Samoa</name>
</country>
<country>
<iso>AD</iso>
<name>Andorra</name>
</country>
etc, etc, ....
I:xmlファイルは次のようになります。上記のxamlでSelectedIndex="{Binding CountryIndex}"
を使用してこれを達成しました。
そして問題が始まりました。彼はPerson
モデルまたはPersonListViewModel
の何かを呼び出すコンボボックスの国の選択。 P
解決の鍵は、このコンボボックスにバインドされていることは明白である:私は...ほとんど何を試してみました
SelectedItem="{Binding Path=XmlCountry, Mode=TwoWay}"
プロパティ「XmlCountry」ここPerson
モデルに存在します。私は運がないPersonListViewModel
に入れようとしました。 "Save Person"ボタンは正常に機能します。これは "SelectedPerson"バウンドプロパティをとり、データベースに送信します。それは更新されたコンボボックス値を取得しないことを除いて。
私はコンボボックスのSelectedItem/SelectedIndex
バインディングについて助けを求めます。その他の提案:モデルクラスをラップするにはPersonViewModel
が必要ですか? xml-fileからPersonListViewModel
の "AllCountries"属性を作成し、それをxml-fileの代わりに直接使用する必要がありますか?
ありがとうございます!
UPDATE:私は疑いがあるとして
:悪魔はSelectedItem="{Binding Path=XmlCountry, Mode=TwoWay}"
設定していました。
私がに変更する場合:
SelectedItem="{Binding XmlCountry, **UpdateSourceTrigger=PropertyChanged**}"
すべてが正常に動作します。私は今私の "人を救う"方法に適切なデータを渡しています。しかしながら;私がUpdateSourceTrigger
を設定して、同期してビューとビューモデルを保持しなければならなかったのは初めてです....