2017-08-15 19 views
0

私は申し訳ありませんが、私は間違っていたいくつかの事があるかもしれないので、最初の流れの上にスタックで質問しています。ComboBoxをWPFでバインドしてSelectedItemを取得するにはどうすればよいですか?

私はWPF MVVMの学習を始めましたが、私は立ち往生しています。

データベースには1つのパラメータマスターテーブルがあり、カラムはparameterid,parameternameparametertypeです。それはマスタデータが入力されています。たとえば、国マスターはparametertype "国"、都市は "都市" parametertypeですが、parameteridは一意になります。

私はcountryIdCityIdのパラメータを顧客オブジェクト内に保存するcustomerviewmodelオブジェクトの顧客ページを持っています。私はXAMLからcustomer.CountryIdと直接このparameterIdをバインドするにはどうすればよい

  1. ?それとも不可能ですか?

    現在、私は以下のように達成しています:CountrySelectedItemプロパティを定義し、コンボボックスSelectedItemでバインドしました。そのプロパティが変更されたときに、誰かがその値を変更すると、ビューモデルで私は顧客のCountryIdCountrySelectedItemプロパティに設定しました。

  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にバインドする方法と検証の2番目の問題については、まずSelectedCountryがnullまたは空であるかどうかを確認し、検証エラーメッセージを表示してエラーを出します。それ以外の場合はCustomer.CountryId = SelectedCountryと設定して顧客を保存します。

+0

あなたの貴重なsuggetionと助けてくれてありがとう、しかし私は私の質問を更新したばかりです –

関連する問題