【问题标题】:DevExpress LookupEdit setting EditValue does not workDevExpress LookupEdit 设置 EditValue 不起作用
【发布时间】:2017-08-29 15:19:00
【问题描述】:

我试图让 LookUpEdit 在显示表单时显示初始值。我将国家/地区列表绑定为数据源,然后在表单加载时设置 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 并将数据绑定设置为它自己的EditValue。无论我尝试什么,加载表单时 LookUpEdit 始终为空白,我需要将其设置为initialCountry。我确信这很简单,但我没有看到它,所以非常感谢任何帮助。

【问题讨论】:

    标签: c# devexpress


    【解决方案1】:

    除了马尔科的回答:

    查找中有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 属性。

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

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

      // field names delimited with the ';' character
      this.city.Properties.KeyMember = "CountryId;RegionId;CityName";

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

      // The CountryId value is enough for match.
      // All the another fields(e.g. CountryName) can be skipped while loading
      this.country.EditValue = new Country() { CountryId = 5 }

    【讨论】:

    • 谢谢你,我不知道这个功能!
    • 是的,AFAIK DX 家伙一直在不断改进所有 WinForms 产品线,令我惊喜的是 LookUpEdit 是具有一些新技巧的老狗之一))
    • 谢谢,这看起来很有用!
    • 设置KeyMember 似乎是个技巧,糟糕的是,这不在我一直在寻找的文档中
    【解决方案2】:

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

    this.country.EditValue = initialCountry.CountryId;
    

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

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

    【讨论】:

    • 感谢您的回答。有趣的是,这使得初始值起作用,但现在提交表单时,只有 Country 类上的 CountryId 更新为正确值。我希望整个对象会更新为 LookUpEdit 中选定的 Country 对象。有什么想法吗?
    • 感谢您的帮助,已解决!
    【解决方案3】:

    编辑值需要具有与数据库中相同的数据类型的数据, 所以用适当数据类型的转换函数来分配数据

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-13
      • 1970-01-01
      • 2019-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多