[英]WPF MVVM ComboBox Tag Selection
我有一個ComboBox,它有一個聲明的ComboBox.Items列表(換句話說,不是通過ItemsSource動態綁定)。 我使用ComboBoxItem.Content作為顯示名稱,使用ComboBoxItem.Tag作為相應的Id,如下所示。
如何獲取所選項目的標簽而不是內容? 我嘗試了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>
如果您在ViewModel類中具有此屬性:
private string _serviceType;
public string ServiceType
{
get { return _serviceType; }
set { _serviceType = value; }
}
當然你可以擁有int類型的屬性,它也會工作。
試試這個綁定:
<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>
給組合框一個名字“x:Name =”abcComboBox“然后在代碼端字符串標簽=(abcComboBox.SelectedItem as ComboBoxItem).Tag.ToString();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.