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.
Related
I have a list of check boxes and a single check box that acts as CheckAll/UncheckAll for all items in the listbox.
How can I make the chkAll Check Box checked if all the items are checked in the listbox binding ?
I could create a property in the view model to achieve that. I would like to do it in the xaml code.
<ListBox Name="listBox" ItemsSource="{Binding mySource}">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsSelectedProperty}">
</CheckBox>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<CheckBox Name="chkAll" Content="CheckAll/UnCheckAll" IsChecked ={}/>
I think you will need to have a higher-level view model property that reflects this additional state. The state has three possible values too, so be sure to set IsThreeState to true, and bind it to a Nullable<bool>.
Then in this property's setter, you can update all the other checkbox properties.
For each item's IsSelectedProperty, you'll need to update the three-state property too.
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.
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).
I have a collection of Contact objects that I've bound as follows in a WPF form:
<ComboBox Name="Name"
Text="{Binding Path=Contact.FullName}"
ItemsSource="{Binding ContactsCollection}"
SelectedItem="{Binding Path=Contact, Mode=TwoWay}"
IsEditable="true"
IsTextSearchEnabled="True"
TextBoxBase.TextChanged="Name_TextChanged"/>
<TextBox Name="Position" Text="{Binding Path=Contact.Position}"/>
<TextBox Name="Phone" Text="{Binding Path=Contact.PhoneNumber}"/>
I'd like the contact to be selected when the user starts typing in the combo 'IsTextSearchEnabled=true'.
The problem is that I'd like the items in the collection to remain read-only. Once a contact has been selected, any text deletes or additions modify the contact name in the collection.
How can I bind a collection to a combobox, enable search and prevent edits to the collection?
I could be missing something here, but if you don't want an editable ComboBox, try not setting the ComboBox.IsEditable property to True. Using this simple code, I can display items in a ComboBox and make selections by typing (when the ComboBox is focused) without editing anything:
<ComboBox ItemsSource="{Binding Items}" IsTextSearchEnabled="True" Height="25" />
Thanks for your input. It sorted me out. Removing the SelectedItem property and setting Position and PhoneNumber in the PropertyChanged event was what I needed.
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" />