【问题标题】:How do I bind ComboBox with WPF and get SelectedItem?如何将 ComboBox 与 WPF 绑定并获取 SelectedItem?
【发布时间】:2018-01-23 14:35:39
【问题描述】:

很抱歉,我第一次问关于堆栈溢出的问题,所以可能是我做错了什么。

我已经开始学习 WPF MVVM 并且卡住了。

我在数据库中有一个参数主表,它有 parameterid、parametername、parametertype 列。它填充了主数据,例如,国家主数据有parametertype“国家”,城市有“城市”parametertype,但parameterid 将是唯一的。

现在我有一个客户页面,在 customerviewmodel 对象中我有 countryId 和 CityId 参数要保存在客户对象中。

  1. 如何将这个parameterId 直接与来自XAML 的customer.CountryId 绑定?还是不可能?

    目前我已经实现了如下:定义了一个CountrySelectedItem属性并将其与组合框SelectedItem绑定。当它的属性改变时,当任何人改变它的值时,我在视图模型中设置客户的CountryId CountrySelectedItem 设置属性。

  2. 如何验证客户的 countryID 属性?在组合框中,我已经绑定了 Parameter Master 的对象,所以无法将数据 Annotation 的 Required 属性写入 Parameter Master Entity。

这里是完整的场景
公共类客户 {

    public int CustomerId { get; set; }
    [Required]
    public string CustomerName { get; set; }
    [Required]
    public int CountryId { get; set; }
}
<telerik:RadComboBox x:Name="cmCountryId" Grid.Row="3" Grid.Column="1" HorizontalAlignment="Left" 
                                                     ItemsSource="{Binding LstCountry}"
                                                     HorizontalContentAlignment="Left" VerticalAlignment="Center"  
                                                     DisplayMemberPath="ParameterName" 
                                                     SelectedItem="{Binding SelectedCountry,Mode=TwoWay}"  Height="25" Width="200" >
 </telerik:RadComboBox>


 /* To Fill DropDown Country DropDown From ParameterMaster*/
 public List<ParameterEntity> LstCountry { get; set; }

 LstContainerType = new List<ParameterEntity>(parameterService.GetParamterTypeDetail("COUNTRY").ToList());
 /*
  Parameter Master has ParameterId,ParameterName,ParameterType -- All Master Data Stored Inside It with Different ParameterType.      
  */
/* When User Select CountrId then I Set It's ParameterID As CountryID In My CustomerEntity  */
  public ParameterEntity SelectedCountry
    {
        get
        {
            return _selectedCountry;
        }
        set
        {
            _selectedCountry = value;
            RaisePropertyChanged("SelectedCountry");
            SelectedCustomerEntity.CountryId = _selectedCountry != null ? _selectedCountry.ParameterId : 0;
        }
    }
So Here Is my question for above Scneraio.

1) To Bind Customer Object's CountryId Property From CountryDropDown is that any other option available then this one.
   SelectedCustomerEntity.CountryId = _selectedCountry != null ? _selectedCountry.ParameterId : 0;
   //  please have a look SelectedCountry property.
   Something like , I do not need to write Selected Property and i Can Directly Set ParameterId To CountryId Of CustomerEntity From XAML
   Or this one is right scneario.

2) Another Question Is How To do Validation On CountryId ComboBox.
   i mean As Mention in CustomerEntity has Required CountryId But In XAMl Design of  SelectedItem="{Binding SelectedCountry,Mode=TwoWay}" 
   What should i write to display if user has not select any country from dropdown. Should I write a logic on Save Button manully ?

【问题讨论】:

    标签: wpf validation mvvm data-binding combobox


    【解决方案1】:

    我首先假设您创建了一个表单,上面有一个提交按钮,它将以某种方式保存选定的数据。

    对于组合框,您可以使用 SelectedValuePath 从您绑定的类型中选择一个字段作为您的选择。

    <ComboBox SelectedValue="{Binding SelectedCountry}"
        ItemsSource="{Binding Countries}"
        SelectedValuePath="ParameterId" />
    

    然后您的视图模型中的 SelectedCountry 属性将设置为 ParameterId。

    对于验证的第二个问题以及如何在模型上的提交函数中绑定Customer.CountryId,您将首先检查 SelectedCountry 是否为空或空,然后显示验证错误消息。否则设置Customer.CountryId = SelectedCountry 并保存客户。

    【讨论】:

    • 感谢您的宝贵建议和帮助,但我已经更新了我的问题,只是添加了场景,请告诉我是否可以
    猜你喜欢
    • 2011-11-01
    • 2015-06-28
    • 2010-10-24
    • 1970-01-01
    • 2021-10-18
    • 2017-11-09
    • 2019-10-01
    • 2012-06-23
    • 1970-01-01
    相关资源
    最近更新 更多