2017-08-29 203 views
0

我试图让LookUpEdit在窗体显示时显示初始值。我绑定了一个国家列表作为数据源,然后在表单加载时设置EditValue,这应该在国家LookUpEdit中显示该国家。不幸的是,它只是显示了一个空值。 LookUpEdit似乎不起作用,并允许我滚动浏览国家/地区列表并选择一个项目,并在提交表单时传回该值。DevExpress LookupEdit设置EditValue不起作用

乡村类:

public class Country 
{ 
    public Country(); 
    public int CountryId {get; set;} 
    public string CountryName {get; set;} 
    public string IsoCode {get; set; } 
} 

含有LookUpEdit形式背后的代码:

this.country.Properties.DataSource = this.Countries; 
this.country.Properties.DisplayMember = "CountryName"; 
this.country.Properties.ValueMember = "CountryId"; 

this.country.EditValue = initialCountry; 
this.country.DataBindings.Add("EditValue", viewModel.Address, "AddressCountry", false, DataSourceUpdateMode.OnPropertyChanged); 

在这个例子中this.Countries是一个已填充List<Country>initialCountry设定为CountryviewModel.Address一个实例包含财产Country AddressCountry

我试过设置EditValue直接只设置数据绑定到它自己的EditValue。无论我尝试什么,LookUpEdit在表单加载时总是空白,我需要将其设置为initialCountry。我确信这是一件非常简单的事情,但我没有看到它,所以任何帮助都非常感激。

回答

2

您不应将this.country.EditValue设置为Country的实例,而是设置为CountryId,因为这是您的ValueMember

this.country.EditValue = initialCountry.CountryId; 

编辑:如果你想选择的对象,你应该使用GetDataSourceRowByKeyValue

var selectedCountry = this.country.GetDataSourceRowByKeyValue(this.country.EditValue) as Country; 
+0

感谢您的回答。有趣的是,当表单提交时,只有Country类的CountryId更新为正确的值,现在已经使初始值工作了。我希望整个对象将更新到LookUpEdit中的选定Country对象。有任何想法吗? – peacemaker

+0

我已经更新了答案 –

+0

感谢您的帮助,修复了它! – peacemaker

2

除了马尔科的回答是:

还有就是data binding to the entire business objects在查找一个特殊的模式:

this.country.Properties.DataSource = this.Countries; 
this.country.Properties.DisplayMember = "CountryName"; 
this.country.Properties.KeyMember = "CountryId"; 
this.country.EditValue = initialCountry; 

此模式允许查找机制通过将关键字段(“CountryId”)分配给RepositoryItemLookUpEditBase.KeyMember属性,在查找数据源中找到编辑器值(Country业务对象)与另一个Country业务对象之间的匹配。

下面是此模式的一些额外的好处:

  • 可以使用多个关键字字段(“复合/化合物键”功能);

    //用';'分隔的字段名称字符
    this.city.Properties.KeyMember =“CountryId; RegionId; CityName”;

  • 可以匹配的业务对象,从不同的数据的上下文加载,并使用延迟加载方法的所有优点:

    //该CountryId值是足够的匹配。
    //在加载
    this.country时,可以跳过所有其他字段(例如国家/地区名称)。EditValue = new Country(){CountryId = 5}

+0

谢谢你,我不知道这个功能! –

+0

是的,AFAIK DX家伙不断改进所有的WinForms产品系列,我一直惊喜地看到LookUpEdit是一些带有一些新花样的老狗) – DmitryG

+0

谢谢,这看起来很有用! – peacemaker