Binding Combobox SelectedValue from bound value in UserControl grid

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?


Your XAML is confusing, partly because you are writing bindings the long way, however if everything is working then I suspect you're just missing the DataContext in your binding Path

Here's an example

ViewModel:

List<State> States;
Clinic SelectedClinic;

Where State has two properties

string Abbrev
int StateId

And Clinic has two properties

string Name
int StateId

XAML:

<Grid x:Name="SomePanel" DataContext="{Binding MyViewModel}">

    <Grid DataContext="{Binding SelectedClinic}">

        <TextBox Text="{Binding Name}" />

        <ComboBox ItemsSource="{Binding ElementName=SomePanel, Path=DataContext.States}"
                  DisplayMemberPath="Abbrev" 
                  SelectedValuePath="StateID"
                  SelectedValue="{Binding StateId}" />
    </Grid>
</Grid>

Few things to note here

  • The Parent Grid's DataContext is the ViewModel. The child Grid gets it's DataContext bound to SelectedClinic, which is the Clinic Object. This allows the binding for TextBox.Text and ComboBox.SelectedValue to work.

  • To bind the ComboBox's ItemsSource, I am using ElementName to point the binding to the UI object named SomePanel , and then telling it to bind to DataContext.States . This means the final binding for the ItemsSource is pointing to SomePanel.DataContext.States .


  • If you do have your DataContext set up correctly as you claim, then just remove the ElementName from the binding. That is used for binding to another UIElement, and you don't have any UIElements called ClinicInfoRoot .

    链接地址: http://www.djcxy.com/p/7760.html

    上一篇: 绑定到DataTemplate中的ToString()方法

    下一篇: 从UserControl网格中的绑定值绑定Combobox SelectedValue