在数据网格内操作时,我一直在努力保存组合框选定的值。当我制作没有数据网格的测试解决方案时,一切正常。上下文是与相关国家的人名。国家/地区存储在 xml 文件中。这是初始 View 的快照:
您可以在此处看到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 填充,该模型具有私有(private) ObservableCollection<Person> _persons
实现 INotifyPropertyChanged 的属性,并且是网格的 ItemsSource。您还可以看到SelectedItem="{Binding SelectedPerson}"
在网格中。该部分工作正常。
Person 模型类具有 CountryIndex(字符串,xml 文件中的索引,已计算)、Country(字符串,国家/地区名称),现在我实现了 XmlCountry 属性(XmlElement,xml 文件中的 xmlnode。xml文件看起来像这样:
?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, ....
当我在 ViewModel 构造函数中加载人员时,人员的国家/地区名称用于计算国家/地区索引,该索引用于设置初始值,如屏幕截图中所示。我通过使用 SelectedIndex="{Binding CountryIndex}"
实现了这一点在上面的 xaml 中。
然后问题就开始了;我无法在组合框中选择国家/地区来调用 Person
上的任何内容型号或 PersonListViewModel
。我几乎尝试过任何东西......:P
很明显,解决方案的关键是组合框中的绑定(bind):
SelectedItem="{Binding Path=XmlCountry, Mode=TwoWay}"
此处的属性“XmlCountry”位于 Person
模型。我尝试将其放入 PersonListViewModel
没有运气。 “保存人员”按钮工作正常 - 它采用“SelectedPerson”绑定(bind)属性并将其发送到数据库。只是它没有获取更新的组合框值。
如果您对SelectedItem/SelectedIndex
有任何帮助,我将不胜感激。绑定(bind)在组合框中。还有其他建议:我需要 PersonViewModel
包裹 Person
也许是模特类?我应该在我的 PersonListViewModel
上设置“AllCountries”属性吗?来自 xml 文件并直接使用它而不是 xml 文件?
更新:
正如我所怀疑的:魔鬼就在 SelectedItem="{Binding Path=XmlCountry, Mode=TwoWay}"
设置。
当我更改为:
SelectedItem="{Binding XmlCountry, **UpdateSourceTrigger=PropertyChanged**}"
一切正常。我现在将正确的数据传递给我的“Save Person”方法。然而,这是我第一次必须设置 UpdateSourceTrigger
保持 View 和 View 模型同步......
最佳答案
只需引用上述问题的更新并将其放入实际答案中即可。
The devil was in the SelectedItem="{Binding Path=XmlCountry, Mode=TwoWay}" setting.
When I change the Selected item binding to:
SelectedItem="{Binding XmlCountry, **UpdateSourceTrigger=PropertyChanged**}"
... everything works fine.
关于wpf - MVVM WPF ComboBox SelectedItem 绑定(bind)未在数据网格内激活,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18473674/