绑定到Silverlight中的组合框

时间:2012-07-03 11:39:17

标签: silverlight

我有一个组合框,其中包含Country对象的集合作为它的源(它绑定到视图模型中的Countries属性)。

国家/地区组合显示在用户表单的一部分上。用户表单是从用户对象填充的。用户对象仅包含CountryID(即Country的外键)。我目前通过将名称文本框绑定到我的用户对象中的name属性来填充我的用户表单。但是,当我来到国家组合绑定时,我真的卡住了。

我对Combo的绑定是:

ItemsSource="{Binding Countries}" DisplayMemberPath="CountryDescription" 

因此,对于当前加载的用户,我需要获取其国家ID并以某种方式将其绑定到Country组合?我怎么能这样做,因为国家组合没有一个int列表,而是一个Country对象列表。我虽然关于使用转换器,但这看起来有点矫枉过正,因为组合在其源代码中有一个国家/地区对象,具有我想要的相应CountryID。

那么有没有办法让它让用户CountryID属性绑定到Country组合并获得Country组合,如果友好的用户名?我需要双向绑定,因为用户需要能够选择不同的国家/地区,然后应该更新用户对象中相应的countryID属性。

任何帮助非常感谢!干杯...

修改

这是我所得到的缩减版本(为了清晰起见,我省略了所有notifypropertychanged代码)

class User
{
    public int UserID 
    {
        get;set;
    }

    public string Username
    {
        get;set;
    }

    public int CountryID
    {
        get;set;
    }
}

class Country
{
    public int CountryID
    {
        get;set;
    }

    public string CountryDescription
    {
        get;set;
    }
}

我的视图模型有一个Countries属性,它只是Country对象的列表(上面显示的绑定)。该视图具有用户名的文本框和用于显示国家/地区描述的组合框。我的视图模型有一个" User"要绑定的视图的属性。用户名的绑定是:

<TextBox x:Name="NameBox" Text=" {Binding User.Username, Mode=TwoWay}"   
DataContext="{Binding}" />

我遇到的问题是国家组合的选定项的绑定。我们假设我在组合中有两个国家/地区对象,如下所示:

CountryID = 1,CountryDescription =&#34; France&#34; CountryID = 2,CountryDescription =&#34; Spain&#34;

用户设置为:

UserID = 1,Username =&#34; Bob&#34;,CountryID = 1.

组合需要显示&#34;法国&#34;。但是,如果用户将法国更改为西班牙,则用户的CountryID需要更改为2.

2 个答案:

答案 0 :(得分:0)

基本上你需要一个ValueMemberPath ,它确实存在于组合框,大概是因为他们认为不需要

相反,combox中的每个项都绑定到Country项,因此您真正需要的是一个值转换器,它将Country对象转换为ID号/ int(如果您需要双向,则返回,否则下面的代码将完成这项工作。)

e.g。类似的东西:

public class CountryConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        Country country = (Country)value;
        return country.CountryId;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        /// Not sure yet the best way to get the correct Country object instance, unless you can attach it to the convertor, pass it as a ConvertorParameter or just make it global. :)
    }
}

双向问题是,要从数字转换回Country对象的正确实例,您需要访问控件使用的Country对象的实际列表。

无论如何:然后你只需将ComboBox的SelectedItem绑定到User.CountryID属性(当然也指定转换器)。

<TextBox x:Name="NameBox" SelectedItem={Binding User.CountryID, Convertor={StaticResource CountryConverter}} Text="{Binding User.Username, Mode=TwoWay}" DataContext="{Binding}" />

并在页面资源中声明转换器,如:

<UserControl.Resources>
  <local:CountryConverter x:Key="CountryConverter" />
</UserControl.Resources>

答案 1 :(得分:0)

private Country _SelectedCountry;
public Country SelectedCountry
{
    get
    {
        return _SelectedCountry;
    }

    set
    {
        if(value != null && value != _SelectedCountry)
        { 
            if(User.CountryID != value.CountryID) 
                User.CountryID = value.CountryID;
            _SelectedCountry = value;
        }
    }
}

不要忘记将RaisePropertyChanged放置在所有需要的位置。 如果您对此有任何疑问,请告诉我。

相关问题