2012-11-14 29 views
7

宣言されたComboBox.Itemsリストを持つComboBoxがあります(つまり、ItemsSourceで動的にバインドされていません)。表示名にはComboBoxItem.Contentを使用し、下に示すように対応するIDにはComboBoxItem.Tagを使用します。WPF MVVM ComboBoxタグの選択

どのようにして、選択したアイテムのタグを取得できますか?コンテンツではありません。私はSelectedItemValuePath="Tag"を試しましたが、それは動作しません。

<ComboBox Visibility="{Binding Path=ShowOutpatientFields, Converter= 
     {StaticResource 
      boolTovisConverter}}" Grid.Row="5" Grid.Column="2" Margin="0,2,0,2" 
     Text="{Binding Path=NewCase.ServiceType, ValidatesOnDataErrors=true, 
     NotifyOnValidationError=true}" SelectedValuePath="Tag"> 
      <ComboBox.Items> 
      <ComboBoxItem Content="Hospice" Tag="33" /> 
      <ComboBoxItem Content="Hospital Outpatient" Tag="36" /> 
      <ComboBoxItem Content="Hospital Inpatient Extension" Tag="128" /> 
      <ComboBoxItem Content="Maternity" Tag="52" /> 
      </ComboBox.Items> 
    </ComboBox> 

答えて

8

あなたのViewModelクラスでこのプロパティを持っている場合:

private string _serviceType; 
public string ServiceType 
{ 
    get { return _serviceType; } 
    set { _serviceType = value; } 
} 

もちろんあなたはint型のプロパティを持つことができ、それはあまりにも働くことになります。

このバインディングを試してください)abcComboBox "と、コード側の 文字列タグ=(ComboBoxItemとしてabcComboBox.SelectedItem).Tag.ToString(:

<ComboBox VerticalAlignment="Center" Margin="0,2,0,2" 
       SelectedValue="{Binding ServiceType}" 
       SelectedValuePath="Tag"> 
      <ComboBox.Items> 
       <ComboBoxItem Content="Hospice" Tag="33" /> 
       <ComboBoxItem Content="Hospital Outpatient" Tag="36" /> 
       <ComboBoxItem Content="Hospital Inpatient Extension" Tag="128" /> 
       <ComboBoxItem Content="Maternity" Tag="52" /> 
      </ComboBox.Items> 
     </ComboBox> 
+1

完璧!ありがとう、一束! – NickV

+0

あなたは大歓迎です:) – kmatyaszek

0

名前「名前= Xを" コンボボックス与えます;

関連する問題