I'm used to doing things like
State.Items.Add(new ListItem { Text = "SomeState", Value = NumericIDofState });
Where State is a Listbox in ASP.NET.
How do i achieve the same with a WPF ComboBox? I do see a property called 'Content' in the ComboBoxItem object but how do i assign each item a value other than what's displayed to the user? Please help.
You can display the selected value from combobox to textbox by using the SelectedValuePath and SelectedValue property of WPF ComboBoxAdv control.
On button click event handler, we add the content of TextBox to the ComboBox by calling ComboBox. Items. Add method. Now if you enter text in the TextBox and click Add Item button, it will add contents of the TextBox to the ComboBox.
WPF Combobox has:
SelectedValuePath
property that specifies the path to the property that is used to determine the value of the SelectedValue
property. It's similar to ASP.NET ListItem
's Value
property.DisplayMemberPath
property that defines a default template that describes how to display the data objects. It's similar to ASP.NET ListItem
's Text
property.Let's say you want your Combobox
to show a collection of the following KeyValuePair
objects:
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"), };
You define a property in your view model returning that collection:
public KeyValuePair<int, string>[] TripLengthList { get { return tripLengthList; } }
Then, your XAML for the Combobox
would be:
<ComboBox SelectedValue="{Binding FilterService.TripLengthFrom, Mode=TwoWay}" ItemsSource="{Binding TripLengthList, Mode=OneTime}" SelectedValuePath="Key" DisplayMemberPath="Value" />
Where you set SelectedValuePath
and DisplayMemberPath
properties to the desired property names of the objects (Key
and Value
correspondingly) displaying by the Combobox
.
Or, if you really want to add items to Combobox
in code behind instead of using a binding, you can do it as well. For example:
<!--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")); }
If you only want to expose a simple property in the viewmodel and handle the text for the choices in the view you can do a simple solution like this:
<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>
Example with a bool property:
<ComboBox SelectedValuePath="Tag" SelectedValue="{Binding IsActive, Mode=TwoWay}"> <ComboBoxItem Content="No" Tag="False"/> <ComboBoxItem Content="Yes" Tag="True"/> </ComboBox>
Type-verbose alternatives (original examples)
Below are more verbose alternatives where the types are explicitly declared. Depending on your preferred style (or maybe some types that requires it), maybe it suits you better.
<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>
Example with a bool property:
<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>
The sys namespace is declared as this:
xmlns:sys="clr-namespace:System;assembly=mscorlib"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With