MVVM WPF ComboBox SelectedItem绑定未在datagrid内部激活 [英] MVVM WPF ComboBox SelectedItem Binding not activated inside datagrid

查看:82
本文介绍了MVVM WPF ComboBox SelectedItem绑定未在datagrid内部激活的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have struggled to save my combobox selected value when operating inside a datagrid. When I make a test solution with no datagrid things are working ok. The context are person names with associated countries. The countries are stored in a xml file. Here is a snapshot of the initial view:

您在这里看到(PersonList.xaml的(重要部分):

You see here the (important parts of the)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>.实现INotifyPropertyChanged的_persons 属性,它是网格的ItemsSource.您还会在网格中看到 SelectedItem ="{Binding SelectedPerson}" .那部分工作正常.

This grid is populated from a PersonListViewModel, that has a private ObservableCollection<Person> _persons attribute that implements INotifyPropertyChanged and is the ItemsSource for the grid. You also see the SelectedItem="{Binding SelectedPerson}" in the grid. That part works OK.

Person模型类具有CountryIndex(字符串,xml文件中的索引,已计算),Country(字符串,国家名称),现在我实现了XmlCountry属性(XmlElement,即xml文件中的xmlnode.文件看起来像这样:

The Person model class has CountryIndex (string, index in the xml-file, computed), Country (string, the country name) and now i implemented the XmlCountry attribute (XmlElement, the xmlnode in the xml-file. The xml file looks like this:

?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构造函数中时,该人的国家/地区名称用于计算国家/地区索引,该国家索引用于设置初始值,如屏幕快照所示.我是通过在上面的xaml中使用 SelectedIndex ="{Binding CountryIndex}" 来实现这一目标的.

When I load the persons in the ViewModel constructor the person's country name is used to compute the country index that is used to set the initial values as you see in the screen shot. i achieved this by using the SelectedIndex="{Binding CountryIndex}" in the xaml above.

然后问题开始了;我无法在组合框中选择国家/地区来调用 Person 模型或 PersonListViewModel 上的任何内容.我已经尝试了几乎所有东西...:P

And then the problem started; I can not get the selection of countries in the combobox to call anything on the Person model OR the PersonListViewModel. I have tried almost anything ... :P

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

It is obvious that the key to the solution is this binding in the combobox:

SelectedItem="{Binding Path=XmlCountry, Mode=TwoWay}"

此处的"XmlCountry"属性位于 Person 模型中.我试图把它放在 PersonListViewModel 中,但没有运气.保存人"按钮可以正常工作-它具有"SelectedPerson"绑定属性并发送到数据库.除了无法获取更新的组合框值.

The property 'XmlCountry' here resides in the Person model. I have tried to put it in the PersonListViewModel with no luck. The "Save Person" button works ok - it takes the "SelectedPerson" bound property and sends to the database. Except that it does not get the updated combobox value.

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

I would apperciate any help on the SelectedItem/SelectedIndex binding in the combobox. And also other suggestions: Do I need a PersonViewModel to wrap the Person model class maybe? Should I make a "AllCountries" attribute on my PersonListViewModel from the xml-file and use that instead of the xml-file directly?

非常感谢!

更新:

我怀疑:恶魔位于 SelectedItem ="{Binding Path = XmlCountry,Mode = TwoWay}" 设置中.

As I suspected: The devil was in the SelectedItem="{Binding Path=XmlCountry, Mode=TwoWay}" setting.

当我更改为:

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

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

everything works fine. I am now passing the right data to my "Save Person" method. However; it's the first time I have had to set UpdateSourceTrigger to keep view and viewmodel in synch ....

推荐答案

只需将上述问题的更新引述,并将其放入实际答案中即可.

Just para-quoting the update from the question above and putting it inside an actual answer.

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

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.

这篇关于MVVM WPF ComboBox SelectedItem绑定未在datagrid内部激活的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆