如何将ComboBox与WPF绑定并获取SelectedItem?

时间:2017-08-15 16:04:02

标签: wpf validation mvvm data-binding combobox

我很抱歉我第一次在堆栈上问问题,所以可能是我做错了。

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

我在数据库中有一个参数主表,它有parameteridparameternameparametertype列。它填充了主数据,例如,国家/地区主数据有parametertype"国家&#34 ;,城市有"城市" parametertype,但parameterid将是唯一的。

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

  1. 如何将此parameterId直接与来自XAML的customer.CountryId绑定?或者那是不可能的?

    目前,我已按如下方式实现:定义了一个CountrySelectedItem属性并将其与组合框SelectedItem绑定。当其属性发生变化时,当任何人更改其值时,请在viewmodel中设置客户CountryId设置属性中的CountrySelectedItem

  2. 如何验证客户的countryID媒体资源?在组合框中,我绑定了Parameter Master的对象,因此无法将数据Annotation的Required属性写入参数主实体。

  3. 以下是完整的场景
        公共级客户     {

        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 ?
    

1 个答案:

答案 0 :(得分:0)

我首先假设您创建一个表单,该表单上有一个提交按钮,可以将选定的数据保存在某个庄园中。

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

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

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

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

相关问题