在过去的两天里,我一直在努力做到这一点,但我无法弄清楚我错过了什么。我有一个带有网格的 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 的语法是什么?