双向将一个组合框绑定到一个简单的字符串数组 [英] Two-way bind a combobox to a simple string array

查看:108
本文介绍了双向将一个组合框绑定到一个简单的字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的类来提供这样的状态代码:

I have a simple class that provides state codes like this:

public class StateProvider
{
    static string[] _codes = new string[]
    {
        "AL",
        "AK",
        ...
    };

    public string[] GetAll()
    {
        return _codes;
    }
}

我支持该视图的模型类看起来有点像这个:

My model class that supports the view looks a little like this:

public class ProjectModel : ChangeNotifier
{
    StateProvider _states = new StateProvider();

    public ProjectModel()
    {
        Project = LoadProject();
    }

    ProjectEntity _project;
    public ProjectEntity Project
    {
        get { return _project; }

        set
        {
            _project = value;
            FirePropertyChanged("Project");
        }
    }

    public string[] States { get { return _states.GetAll(); } }
}

我的ComboBox XAML看起来像这样:

And my ComboBox XAML looks like this:

<ComboBox SelectedValue="{Binding Project.State, Mode=TwoWay}" SelectedValuePath="{Binding RelativeSource={RelativeSource Self}}" ItemsSource="{Binding States}" />

从UI到实体的绑定工作 - 如果我从组合中选择状态,那么值被推送到项目实体,我可以保存它。但是,如果我关闭并重新加载,状态代码值不会从实体绑定到UI,并且组合没有显示任何内容。然后,当然,一个后续的保存将空白实体的状态值。

The binding works from the UI to the entity - if I select the state from the combo then the value gets pushed to the project entity and I can save it. However, if I shutdown and reload, the state code value doesn't bind from the entity to the UI and the combo shows nothing selected. Then, of course, a subsequent save nulls the entity's state value.

我想要这个非常简单,因为我想显示状态代码并保存状态代码(我不想要显示完整的状态名称)。所以我不想要创建一个具有代码和FullName属性的State类,并且避免使用组合框的SelectedValuePath和DisplayMemberPath属性。

I want this very simple since I want to display state codes and save state codes (I don't want to display the full state name). So I don't want to have to muck with creating a State class that has Code and FullName properties and avoid having to use the SelectedValuePath and DisplayMemberPath properties of the combobox.

编辑:
添加到ProjectModel如何更改通知的代码。请注意,ProjectEntity类也是这样做的。相信我,它的工作。我已经离开了它,因为它也从一个实体基类继承,通过反射来改变通知。

Added to the code how ProjectModel does change notification. Note that the ProjectEntity class does this too. Trust me, it works. I've left it out because it also inherits from an Entity base class that does change notification through reflection. TwoWay binding works on everything but for the combobox.

推荐答案

哇,whodathought它会归结为:

Wow, whodathought it'd come down to this:

<ComboBox ItemsSource="{Binding States}" SelectedValue="{Binding Project.State, Mode=TwoWay}" />

原来,这是我将属性放在XAML中的顺序。 SelectedValue绑定发生在ItemsSource绑定之前,因此在SelectedValue绑定时,组合框中没有任何项目。

It turned out it was the order in which I placed the attributes in the XAML. The SelectedValue binding was happening before the ItemsSource binding and thus there were no items in the combobox when the SelectedValue was bound.

哇,这只是一个非常糟糕的事情。

Wow, this just seems like a really bad thing.

这篇关于双向将一个组合框绑定到一个简单的字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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