I've spent the last two days trying to make this happen, and I can't figure out what I am missing. I have a WPF user control with a grid and within that grid are text boxes and comboboxes. The DataContext of the grid is set in C# code with an object, and I've been able to two-way bind my text boxes to the grid's DataContext object. Here is some sample code. The object that is going into the ClinicInfoRoot is my Clinic object and one of it's properties is StateID (which is important in a moment)
private void Events_ClinicSelected( object sender, ClinicSelectedEventArgs e )
{
if ( !DesignerProperties.GetIsInDesignMode( this ) )
{
// Get the current logged in user object from arguments and set local
this.CurrentLoggedInPDUser = e.CurrentLoggedInPDUser;
// Bind the patient object to the window grid data context
this.ClinicInfoRoot.DataContext = e.Clinic;
// Set the mode and call mode manager
this.SetMode( Mode.View );
this.ModeManager();
}
}
Now for xaml:
<Grid Name="ClinicInfoRoot"
Margin="0,0,10,10"
Validation.Error="ClinicInfoRoot_Error">
<TextBox Margin="82,28,0,0"
Name="txtName"
VerticalAlignment="Top"
HorizontalAlignment="Left"
Width="82" >
<TextBox.Text>
<Binding Path="Name"
Mode="TwoWay"
ValidatesOnDataErrors="True"
ValidatesOnExceptions="True"
NotifyOnValidationError="True"
UpdateSourceTrigger="PropertyChanged"></Binding>
</TextBox.Text>
</TextBox>
<ComboBox HorizontalAlignment="Left"
Margin="281,141,0,0"
Name="cbState"
VerticalAlignment="Top"
Width="73"
ItemsSource="{Binding Mode=OneWay}"
DisplayMemberPath="Abbrev"
SelectedValuePath="StateID" >
<ComboBox.SelectedValue>
<Binding ElementName="ClinicInfoRoot"
Path="Clinic.StateID"
Mode="TwoWay"
ValidatesOnDataErrors="True"
ValidatesOnExceptions="True"
NotifyOnValidationError="True"
UpdateSourceTrigger="PropertyChanged"></Binding>
</ComboBox.SelectedValue>
</ComboBox>
I have been able to bind the textbox with the appropriate properties from the Clinic object, but the issue is with my State combobox. I have bound the ItemsSource with a list of states from another object, and the combobox fills correctly. However, I want the StateID property in the Clinic object to set what is showing in the combobox, but I can't figure out what the ElementName and Path properties should be for the SelectedValue.
What is the syntax for the ElementName and Path in the binding for the SelectedValue of my combobox?