gpt4 book ai didi

c# - DevExpress LookupEdit 设置 EditValue 不起作用

转载 作者:行者123 更新时间:2023-11-30 19:36:26 24 4
gpt4 key购买 nike

我试图让 LookUpEdit 在显示表单时显示初始值。我将国家列表绑定(bind)为数据源,然后在加载表单时设置 EditValue,这应该将国家显示为 LookUpEdit 中的选定项。不幸的是,它只显示一个空值。 LookUpEdit 似乎可以正常工作,它允许我滚动浏览国家/地区列表并选择一个项目,当我提交表单时该值会传回。

国家类:

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设置为 Country 的一个实例和 viewModel.Address包含属性 Country AddressCountry .

我都试过设置 EditValue仅直接并将数据绑定(bind)设置为它自己的 EditValue。无论我尝试什么,加载表单时 LookUpEdit 始终为空白,我需要将其设置为 initialCountry .我确信它非常简单,但我没有看到它,所以非常感谢任何帮助。

最佳答案

除了Marko的回答:

有一个特殊模式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属性(property)。

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

  • 您可以使用多个键字段(“复合键”功能);

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

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

    //CountryId 值足够匹配
    //加载时可以跳过所有其他字段(例如CountryName)
    this.country.EditValue = new Country() { CountryId = 5 }

关于c# - DevExpress LookupEdit 设置 EditValue 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45942804/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com