I have a number of combo boxes in a window that I need to setup two-way binding on, however I can't seem to figure out how to do it. I have successfully set up basic binding to fill the control where I have the ItemsSource="{Binding}" and I set my DataContext in my C# code behind. I also have two-way binding working on all of the other controls in the window
Let's say I have an object and I am returning an ObservableCollection of it and I want to display Name which is a string. I want the value member to be an ID which is an integer. Is there a code snipped you can please provide that shows how to do two way binding on the ID? I can't seem to find a ValueMember property anywhere, and basically XamComboBox binding is a mystery for me.
Thank you for a very simple and basic xaml code snippet.
Krasimir,
Thank you for your sample project, but it is not what I was looking for. Maybe I have not been explaining this in a good way, so let me take a step back and try again. Also I made a mistake and grabbed the composite control, and I meant to use the WPF XamComboEditor, please excuse.
I have a user control with a number of edit controls and XamComboEditor controls. This is a detail window for a patient. I have bound the user control's grid to a Patient object and then did two-way binding between each TextBox and the appropriate property in the Patient object. This all works great.
Next I have several combo boxes that I have filled with information. An example of one of these would be Clinics (I have several others). I have populated all combo boxes when the window inits so they are all filled by the time I bind my Patient object. In filling my combo boxs, I have the ItemsSource="{Binding}" and set their DataContext to an ObservableCollection.
What I am looking to do is if the selected item in a combo box changes, the patient object will be updated through two-way binding as with the text controls. I am new to WPF and binding, and with windows forms, I would have trapped the selection changed event and made the change in there. However I am wondering if there is a way to do this with binding the combo boxes selected item event to the Patient object? Or is it because I have bound the combo boxes to the lists that originally populated them, two-way binding to the Patient object is not possible and I have to take another step in there to make it happen.
Thanks.
Hello Mike,
Thank you for the clarification and the sample code that you have posted. I have looked into it and I assume that you are using the shared XamComboEditor, which is available in both WPF and Silverlight. If my assumption is correct, you can use the SelectedItem in order to bind the name of the selected item form the XamComboEditor to the Text property of the TextBox, since the XamComboEditor which comes from the Silverlight does not currently expose ValuePath property. I have used the sample code that you have posted and created a sample application in which you can change the Name of the selected Clinic from the TextBox.
If you need any further assistance on the matter please do not hesitate to ask.
Sincerely,
Krasimir
Developer Support Engineer
Infragistics
www.infragistics.com/support
Close, if my combo boxes were in a grid, but they are in a window. Here is some sample code. In my xaml I have:
<Label Content="Clinic" Height="28" HorizontalAlignment="Left" Margin="223,113,0,0" Name="lblClinic" VerticalAlignment="Top" Width="90" /> <ig:XamComboEditor HorizontalAlignment="Left" Margin="223,138,0,0" Name="cbClinic" VerticalAlignment="Top" Width="158" DisplayMemberPath="Name" ItemsSource="{Binding}" IsEditable="False" />
In my C# code I am setting the DataContext of the XamComboEditor with an ObservableCollection
this.cbClinic.DataContext = new Clinic().SelfListAll();
So, if I want to set two way binding in a XamComboEditor, I can't find a
ValuePath in a XamComboEditor. Your example will be ultimately helpful
to me, but I'm looking to do this not in a grid, but just with a
standalone XamComboEditor in a window.
Here is an example of what I am doing to bind a text control:
<TextBox Height="23" HorizontalAlignment="Left" Margin="223,26,0,0" Name="txtFirstName" VerticalAlignment="Top" Width="158"> <TextBox.Text> <Binding Path="FirstName" Mode="TwoWay" ValidatesOnDataErrors="True" ValidatesOnExceptions="True" NotifyOnValidationError="True" UpdateSourceTrigger="PropertyChanged"></Binding> </TextBox.Text> </TextBox> Thanks.
<TextBox Height="23" HorizontalAlignment="Left" Margin="223,26,0,0" Name="txtFirstName" VerticalAlignment="Top" Width="158"> <TextBox.Text> <Binding Path="FirstName" Mode="TwoWay" ValidatesOnDataErrors="True" ValidatesOnExceptions="True" NotifyOnValidationError="True" UpdateSourceTrigger="PropertyChanged"></Binding> </TextBox.Text> </TextBox>
I have been looking into the functionality that you are looking for and the XamComboEtidor have a ValuePath property which provides a two way binging to the value of the cell in which the editor is used. For example, you have the following scenario: you have an int field called ItemID which represents a int property in you data source and you wish to display string representation of the integer value of this field. You create class called Item that have a Name and ID properties and you create an ObservableCollection<Item> and set it as ItemsSource to a XamComboEdtior used in the ItemID field. You are setting the DisplayPath to Name in order to display name of the item instead of the integer value of the cells in the field. When you set the ValuePath to ID you will create a two way binging between the ID property of the SelectedItem of the XamComboEditor and the property that is represented by the Cell in which the XamComboEditor is displayed. I have created a sample application that demonstrates the described scenario.
Please let me know if this is what you are looking for or I have misunderstood you in any way.
Looking forward to hearing from you.