[英]WPF combobox value and display text
我習慣這樣做
State.Items.Add(new ListItem { Text = "SomeState", Value = NumericIDofState });
State是ASP.NET中的列表框。
我如何使用WPF ComboBox實現相同的目標? 我確實在ComboBoxItem對象中看到了一個名為“Content”的屬性,但是如何為每個項目分配一個值而不是顯示給用戶的值? 請幫忙。
SelectedValuePath
屬性,指定用於確定SelectedValue
屬性值的屬性的路徑。 它類似於ASP.NET ListItem
的Value
屬性。 DisplayMemberPath
屬性,用於定義描述如何顯示數據對象的默認模板。 它類似於ASP.NET ListItem
的Text
屬性。 假設您希望Combobox
顯示以下KeyValuePair
對象的集合:
private static readonly KeyValuePair<int, string>[] tripLengthList = {
new KeyValuePair<int, string>(0, "0"),
new KeyValuePair<int, string>(30, "30"),
new KeyValuePair<int, string>(50, "50"),
new KeyValuePair<int, string>(100, "100"),
};
您在視圖模型中定義一個返回該集合的屬性:
public KeyValuePair<int, string>[] TripLengthList
{
get
{
return tripLengthList;
}
}
然后,你的Combobox
的XAML將是:
<ComboBox
SelectedValue="{Binding FilterService.TripLengthFrom, Mode=TwoWay}"
ItemsSource="{Binding TripLengthList, Mode=OneTime}"
SelectedValuePath="Key"
DisplayMemberPath="Value" />
將SelectedValuePath
和DisplayMemberPath
屬性設置為Combobox
顯示的對象的所需屬性名稱( Key
和Value
)。
或者,如果你真的想在后面的代碼中添加項目到Combobox
而不是使用綁定,你也可以這樣做。 例如:
<!--XAML-->
<ComboBox x:Name="ComboBoxFrom"
SelectedValue="{Binding FilterService.TripLengthFrom, Mode=TwoWay}" />
// Code behind
public partial class FilterView : UserControl
{
public FilterView()
{
this.InitializeComponent();
this.ComboBoxFrom.SelectedValuePath = "Key";
this.ComboBoxFrom.DisplayMemberPath = "Value";
this.ComboBoxFrom.Items.Add(new KeyValuePair<int, string>(0, "0"));
this.ComboBoxFrom.Items.Add(new KeyValuePair<int, string>(30, "30"));
this.ComboBoxFrom.Items.Add(new KeyValuePair<int, string>(50, "50"));
this.ComboBoxFrom.Items.Add(new KeyValuePair<int, string>(100, "100"));
}
如果您只想在viewmodel中公開一個簡單的屬性並處理視圖中選項的文本,您可以執行如下簡單的解決方案:
<ComboBox SelectedValuePath="Tag" SelectedValue="{Binding YourIntProperty, Mode=TwoWay}">
<ComboBoxItem Content="First choice" Tag="0"/>
<ComboBoxItem Content="Second choice" Tag="1"/>
<ComboBoxItem Content="Third choice" Tag="2"/>
</ComboBox>
bool屬性的示例:
<ComboBox SelectedValuePath="Tag" SelectedValue="{Binding IsActive, Mode=TwoWay}">
<ComboBoxItem Content="No" Tag="False"/>
<ComboBoxItem Content="Yes" Tag="True"/>
</ComboBox>
類型 - 詳細替代(原始示例)
下面是明確聲明類型的更詳細的替代方案。 根據您的喜好風格(或某些需要它的類型),也許它更適合您。
<ComboBox SelectedValuePath="Tag" SelectedValue="{Binding YourIntProperty, Mode=TwoWay}">
<ComboBoxItem Content="First choice">
<ComboBoxItem.Tag>
<sys:Int32>0</sys:Int32>
</ComboBoxItem.Tag>
</ComboBoxItem>
<ComboBoxItem Content="Second choice">
<ComboBoxItem.Tag>
<sys:Int32>1</sys:Int32>
</ComboBoxItem.Tag>
</ComboBoxItem>
<ComboBoxItem Content="Third choice">
<ComboBoxItem.Tag>
<sys:Int32>2</sys:Int32>
</ComboBoxItem.Tag>
</ComboBoxItem>
</ComboBox>
bool屬性的示例:
<ComboBox SelectedValuePath="Tag" SelectedValue="{Binding IsActive, Mode=TwoWay}">
<ComboBoxItem Content="No">
<ComboBoxItem.Tag>
<sys:Boolean>False</sys:Boolean>
</ComboBoxItem.Tag>
</ComboBoxItem>
<ComboBoxItem Content="Yes">
<ComboBoxItem.Tag>
<sys:Boolean>True</sys:Boolean>
</ComboBoxItem.Tag>
</ComboBoxItem>
</ComboBox>
sys命名空間聲明為:
xmlns:sys="clr-namespace:System;assembly=mscorlib"
請參閱組合的這些屬性:
如果跳過Value,那么我認為在運行時將新項添加到ComboBox中非常簡單。
comboBox1.Items.Add("SomeText");
comboBox1.SelectedIndex = comboBox1.Items.Count - 1;
SelectedIndex
屬性設置為Items.Count-1
以便新添加的項目作為選定項目顯示在ComboBox中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.