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

标签 c# devexpress

我试图让 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/

相关文章:

C# Web API LINQ 实体查询 - 将 AnonymousType 转换为类对象

c# - C# 有 make_tuple 吗?

c# - HttpClient 在 using 语句中

c# - 悬停时菜单跳转

c# - 具有动态列和行的 DevExpress WPF GridControl

c# - 将自定义集合转换为 DataRow

c# - 如何处理 GridView 中的行双击?

c# - 如何使用 DevExpress 查找栏搜索但不过滤 GridView 表?

c# - GridControl 的 SelectedMode (DevExpress)

c# - 在中间件中为什么这个值只设置一次?