MVVM WPF ComboBox SelectedItem绑定未在datagrid中激活

Jor*_*enH 11 wpf binding datagrid combobox mvvm

在数据网格内部操作时,我一直在努力保存我的组合框选择值.当我制作一个没有数据网格的测试解决方案时,一切正常.上下文是与相关国家/地区的人名.这些国家/地区存储在xml文件中.以下是初始视图的快照:

Combobox与国名

你在这里看到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>
Run Code Online (Sandbox Code Playgroud)

此网格由PersonListViewModel填充,PersonListViewModel具有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, ....
Run Code Online (Sandbox Code Playgroud)

当我在ViewModel构造函数中加载人员时,该人员的国家/地区名称用于计算用于设置初始值的国家/地区索引,如屏幕截图所示.我通过使用SelectedIndex="{Binding CountryIndex}"上面的xaml 实现了这一点.

然后问题就开始了; 我无法在组合框中选择国家来调用Person模型上的任何东西PersonListViewModel.我几乎尝试过任何事情......:P

很明显,解决方案的关键是组合框中的这种绑定:

SelectedItem="{Binding Path=XmlCountry, Mode=TwoWay}"
Run Code Online (Sandbox Code Playgroud)

这里的属性'XmlCountry'位于Person模型中.我试图把它放在PersonListViewModel没有运气的情况下."保存人员"按钮工作正常 - 它接受"SelectedPerson"绑定属性并发送到数据库.除了它没有得到更新的组合框值.

我会对SelectedItem/SelectedIndex组合框中的绑定提供任何帮助.还有其他建议:我需要一个PersonViewModel包装Person模型类吗?我应该PersonListViewModel从xml文件中创建一个"AllCountries"属性并直接使用它而不是xml文件吗?

非常感谢提前!

更新:

我怀疑:魔鬼在场 SelectedItem="{Binding Path=XmlCountry, Mode=TwoWay}".

当我改为:

SelectedItem="{Binding XmlCountry, **UpdateSourceTrigger=PropertyChanged**}"

一切正常.我现在将正确的数据传递给我的"保存人员"方法.然而; 这是我第一次设置UpdateSourceTrigger保持视图和视图模型同步....

ouf*_*lak 11

只是引用上面问题的更新并将其置于实际答案中.

魔鬼在SelectedItem ="{Binding Path = XmlCountry,Mode = TwoWay}"设置中.

当我将Selected item绑定更改为:

SelectedItem="{Binding XmlCountry, **UpdateSourceTrigger=PropertyChanged**}"
Run Code Online (Sandbox Code Playgroud)

......一切正常