Silverlight combobox selectedvalue string property binding - c#

I have a combobox with items like below:
{[1, US]}
{[2, UK]}
My combobox will display it with the Value. My problem is I can't set the SelectedValue property of my combobox.
<ComboBox Name="cbSource" Grid.Row="1" Grid.Column="3"
ItemsSource="{Binding Datas.Countries, Mode=OneWay}"
SelectedValue="{Binding CurrentObject.Country, Mode=TwoWay}"
DisplayMemberPath="Value" SelectedValuePath="Key"></ComboBox>
Now my CurrentObject.Country is a string property with a value of UK. I also tried this one below but no luck.
DisplayMemberPath="Value" SelectedValuePath="Value"
What can I do here?

It is not possible to achieve your behavior using a key value pair. See the example below.
Just create a class having 2 properties one for key and one for value. Then bind a collection of this class as the itemssource and bind the selectedvalue to a string property. Ie Datas.Countries is the collection of the class.
<ComboBox Name="cbSource" Grid.Row="1" Grid.Column="3"
ItemsSource="{Binding Datas.Countries, Mode=OneWay}"
SelectedValue="{Binding SomePropertyToHoldKeyValue, Mode=TwoWay}"
DisplayMemberPath="Value" SelectedValuePath="Key"></ComboBox>
I think we can understand the difference between SelectedItem, SelectedValue, DisplayMemberPath and SelectedValuePath better with an example. See this class:
public class Employee
{
public int Id;
public string Name;
}
and the following xaml:
<ComboBox ItemsSource="{Binding Source={StaticResource Employees}}"
DisplayMemberPath="Name"
SelectedValuePath="Id"/>
DisplayMemberPath points to the Name property, so the value displayed in the ComboBox and the Employee entries contained in the drop down list, will be the Name property of the Employee object.
To understand the other two, you should first understand SelectedItem. SelectedItem will return the currently selected Employee object from the ComboBox. You can also assign SelectedItem with an Employee object to set the current selection in the ComboBox.
SelectedValuePath points to Id, which means you can get the Id of currently selected Employee by using SelectedValue. You can also set the currently selected Employee in the ComboBox by setting the SelectedValue to an Id (which we assume will be present in the Employees list).

Related

Setting multiple SelectedValue on WPF ComboBox

Note: I'm using Mahapps.Metro if some parameters seem odd.
So, I have a Model called User, which has a StateId.
Combobox is filled with States, so when a state is selected I put the Id of the selected state into User.StateId like this:
<ComboBox
Grid.Column="0"
Margin="0,5,0,0"
mah:TextBoxHelper.ClearTextButton="True"
mah:TextBoxHelper.Watermark="Select a State..."
ItemsSource="{Binding States, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectedItem="{Binding SelectedState, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectedValue="{Binding User.StateId, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectedValuePath="Id" />
And this works just fine. However, what if I wanted to put another parameter of the selected item into the User model? Let's say User also has a property called StateName, and when I select a new State, I pass both the State.Id and State.Name into the User.StateId and User.StateName?
Hopefully this wasn't too confusing. Thanks!
SelectedValue and SelectedItem can only be bound to a single source property. But you should be able to bind SelectedItem to a State property of the User class rather than trying to set both StateId and StateName.
If the User class doesn't have a State property, you could bind to a SelectedState property of a view model (which it seems like you already are) that then in turn sets the StateId and StateName properties of the user. You should only bind to either SelectedItem or SelectedValue anyway.

Updating ComboBox selected item

I have a ComboBox that is that is being populated from an ItemsSource collection. I am populating all the customers into an Observable Collection, and binding it to the collection, like so.
<ComboBox ItemsSource="{Binding Path=Customers}"
SelectedValue="{Binding CustomerKey}"
DisplayMemberPath="FullName" SelectedValuePath="{Binding Key}" />
In this same view, I have a data grid with basic information with orders in it. When an order is selected, within the view model I am getting a more detailed record for the order. What I want to do is update the view with customer information for the newly selected order.
I was trying to set the CustomerKey, and then selected that Customer based on it's key -- I think I am doing this all wrong though. How can I accomplish this?
The SelectedValuePath property of the ComboBox should be set to a string (and not to a binding) that specifies the name of the property of the Customer class where the key is stored:
<ComboBox ItemsSource="{Binding Path=Customers}"
SelectedValue="{Binding CustomerKey}"
DisplayMemberPath="FullName" SelectedValuePath="Key" />
This will work provided that the Customer class has a FullName and a Key property and that the Key property has the same type as the CustomerKey property of the view model.
A customer with a key matching the value that you set the CustomerKey property to must also be present in the Customers collection that you bind the ComboBox to for an item to be selected.

Binding combobox data bult from a list to a property

I have a combo box and button as such
<ComboBox ItemsSource="{Binding MessageTypesList}"
DisplayMemberPath="MessageType"
SelectedValue="MessageType" />
<Button Content="Search"
Command="{Binding Path=SearchMessageTypes}"
x:Name="SearchMessageTypeButton"/>
The MessageTypesList list is generated from a SQL query and once the Message Type is selected from the list the Search button needs to pass the selected value to a string property in my ViewMainModel.
When I debug the application the value passed to the MessageType property is always NULL. I have this working for a similar date time searchs but cant see how to pass the MessageType value in my XAML to the MessageType propery form binding generated lists.
You should bind the SelectedValue property to a property in your viewmodel.
Create a property in your viewmodel:
public MessageType SelectedType {get;set;}
Bind selectedItem to this property in XAML:
<ComboBox ItemsSource="{Binding MessageTypesList}" SelectedItem="{Binding SelectedType, Mode=TwoWay" />

WPF ComboBox doesn't show selection

Scenario:
A list of user control of MyControl type:
public List<MyControl> Controls { get; set; }
public MyControl SelectedControl { get; set; }
A ComboBox with the ItemsSource linked to the Controls property:
<ComboBox ItemsSource="{Binding Path=Controls}" SelectedItem="{Binding Path=SelectedControl}" DisplayMemberPath="HeaderTitle" >
The problem is that the ComboBox shows the items correctly but when I select an Item it doesn't appear in the ComboBox. Why?
PS: HeaderTitle is a DependencyProperty of the MyControl type.
I think this is a duplicate of WPF - Combobox SelectedItem not getting set?
Therefore I'd like to quote Heinz K's answer https://stackoverflow.com/a/3506262/6071619
I had the same problem and solved it by overriding the Equals() Method in my CustomObject and compared the Id Property.
If the item that is selected is not the same instance that is contained in the List, you must override Equals() in the CustomObject to let the ComboBox know that it is the same object.
If it's the same instance, maybe it's only a simple thing such as setting the BindingMode to TwoWay:
SelectedItem="{Binding Path=CustomSettingProperty,Mode=TwoWay}"
try to bind it like this..
<ComboBox ItemsSource="{Binding Controls}" SelectedItem="{Binding SelectedControl, Mode=TwoWay}" DisplayMemberPath="{Binding HeaderTitle}" >
you dont really need to have the selected property bound to its class... it can be just a string also. so just store the selected item in type string and then work on getting the items from your list that match that selected item.
Try setting the DataContext for the ComboBox?
<ComboBox DataContext="{Binding Controls}" ItemsSource="{Binding Controls}" DisplayMemberPath="HeaderTitle">
You shouldn't have to bind the SelectedItem property as long as the ItemsSource and DisplayMemberPath are set.

How to Bind with Combobox in silverlight

I want to bind my collection of countries to a Combobox.
My XAML page looks as follows:
<pmControls:pmComboBox
Margin="3" Grid.Row="5" Grid.Column="1"
ItemsSource="{Binding Path=Countries}"
SelectedValue="{Binding Title,Mode=TwoWay}" >
</pmControls:pmComboBox>
I want to display Title as DataTextField.
Currently it shows namespace of country class in my combobox list .
I have also tried to add DisplayMemberPath but it also not works.
How can I setup the display field of Combobox to use a Binding?
I think you need to do
DisplayMemberPath="Title"
SelectedValuePath="Title"
SelectedValue="{Binding Path=Country,Mode=TwoWay}"

Categories