从UserControl网格中的绑定值绑定Combobox SelectedValue

时间:2011-12-29 20:01:04

标签: wpf binding combobox selectedvalue

我花了两天时间试图让这件事发生,我无法弄清楚我错过了什么。我有一个带网格的WPF用户控件,在该网格中有文本框和组合框。网格的DataContext是用带有对象的C#代码设置的,我已经能够将文本框双向绑定到网格的DataContext对象。这是一些示例代码。进入ClinicInfoRoot的对象是我的Clinic对象,其中一个属性是StateID(暂时很重要)

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();
    }
}

现在为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>

我已经能够使用Clinic对象中的相应属性绑定文本框,但问题出在我的State组合框中。我已将ItemsSource与另一个对象的状态列表绑定,并且组合框正确填充。但是,我希望Clinic对象中的StateID属性设置组合框中显示的内容,但我无法弄清楚SelectedValue的ElementName和Path属性应该是什么。

我的组合框的SelectedValue绑定中ElementName和Path的语法是什么?

2 个答案:

答案 0 :(得分:2)

你的XAML令人困惑,部分原因是你在很长一段时间内编写绑定,但如果一切正常,那么我怀疑你错过了绑定DataContext中的Path

这是一个例子

视图模型:

List<State> States;
Clinic SelectedClinic;

State有两个属性

string Abbrev
int StateId

Clinic有两个属性

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>

这里很少有注意事项

  • Parent Grid的DataContext是ViewModel。子Grid将它的DataContext绑定到SelectedClinic,它是Clinic Object。这允许TextBox.TextComboBox.SelectedValue的绑定起作用。

  • 要绑定ComboBox的ItemsSource,我使用ElementName将绑定指向名为SomePanel的UI对象,然后告诉它绑定到DataContext.States。这意味着ItemsSource的最终绑定指向SomePanel.DataContext.States

答案 1 :(得分:1)

如果您确实按照声明设置了DataContext,那么只需从绑定中删除ElementName即可。它用于绑定到另一个UIElement,并且您没有任何名为ClinicInfoRoot的UIElements。

相关问题