2017-08-15 117 views
0

对不起,我在第一次提问堆栈溢出问题,所以可能是我做错了一些事情。如何使用WPF绑定ComboBox并获取SelectedItem?

我已经开始学习WPF MVVM了,我被卡住了。

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

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

  1. 我如何结合这parameterId直接与customer.CountryId从XAML?或者这是不可能的?

    目前我已经达到它如下:定义了一个CountrySelectedItem属性并用combobox SelectedItem绑定它。当它的属性发生变化时,当任何人改变它的值,然后在viewmodel中,我设置了CountrySelectedItem set属性中的Customer CountryId

  2. 如何验证客户的countryID财产?在组合框中,我绑定了参数主对象,因此无法将数据注释的必需属性写入参数主实体。

下面是完整的方案
公共类客户 {

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 ? 

回答

0

我将让你创建一个表单上会有一个提交按钮,将节省的假设开始在一些庄园中选择的数据。

组合框,您可以使用SelectedValuePath从你作为你选择的装订类型选择字段。

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

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

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

+0

感谢您提供宝贵的建议和帮助,但我更新了我的问题,只是添加了方案,请让我知道是否可以 –