2017-02-28 71 views
0

我有一個實體表,其中列出了ISO國家代碼及其說明以填充組合框。然後我有另一個表格,將「國籍」設置爲ComboBox的選定值。 IsoCountryCodes和我的其他表之間沒有外鍵約束。設置組合框的Selected Item/Text to Value路徑ItemsSource

我想能夠將組合框的SelectedItem設置爲選定的IsoCountryCode,但我想將SelectedValue保存到「國籍」字段。

我試着將ComboBox的SelectedItem設置爲我的'國籍'(字符串)字段,但它只是在IsoCountryCode上做一個ToString()。當我設置「文本」字段而不是SelectedItem時,它將DisplayPath值保存到「國籍」字段。

我想知道是否有方法讓ValuePath值保存到沒有任何外鍵約束的字段,同時仍然向用戶顯示有效的DisplayPath? IsoCountryCode表

例子:

PassengerId PassengerName PassengerNationality PassengerDocumentIssuingCountry 
1   'Carter, John' 'GBR'    'USA' 

而且我目前的XAML:

IsoCode  IsoDescription 

'GBR'  'United Kingdom' 
'USA'  'United States of America' 

其他表的實施例

<xctk:WatermarkComboBox x:Name="uxNationalityCmbo" Margin="0,0,5,5" Watermark="Nationality" 
     Grid.Row="2" ItemsSource="{Binding CountryCodeCollection}" SelectedValuePath="IsoCode" DisplayMemberPath="IsoDescription" 
     IsTextSearchEnabled="True" Text="{Binding SelectedPassenger.PassengerNationality, TargetNullValue=''}"/> 

現在改變選擇的值會到美國會將實體PassengerNational更改爲「美利堅合衆國」,而我需要'USA'

回答

1

您應該SelectedValue屬性綁定到PassengerNationality源屬性:

<xctk:WatermarkComboBox x:Name="uxNationalityCmbo" Margin="0,0,5,5" Watermark="Nationality" 
    Grid.Row="2" ItemsSource="{Binding CountryCodeCollection}" SelectedValuePath="IsoCode" DisplayMemberPath="IsoDescription" 
    IsTextSearchEnabled="True" SelectedValue="{Binding SelectedPassenger.PassengerNationality, TargetNullValue=''}"/>