1

私はこれを実現するために過去2日間を費やしましたが、何が欠けているのか理解できません。グリッドを使用したWPFユーザーコントロールがあり、そのグリッド内にテキストボックスとコンボボックスがあります。グリッドのDataContextは、オブジェクトを使用してC#コードで設定され、テキストボックスをグリッドのDataContextオブジェクトに双方向でバインドすることができました。ここにいくつかのサンプルコードがあります。ClinicInfoRootに入るオブジェクトは私のClinicオブジェクトであり、そのプロパティの1つは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の構文は何ですか?

4

2 に答える 2

2

あなたの XAML は、バインディングを長い道のりで書いているため、混乱していますが、すべてが機能している場合はDataContext、バインディングに .Path

これが例です

ビューモデル:

List<State> States;
Clinic SelectedClinic;

State2 つのプロパティがある場所

string Abbrev
int StateId

そしてClinic2つのプロパティを持っています

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>

ここで注意することはほとんどありません

  • 親グリッドの DataContext は ViewModel です。子グリッドは、Clinic オブジェクトである SelectedClinic にバインドされた DataContext を取得します。TextBox.Textこれにより、とのバインドがComboBox.SelectedValue機能します。

  • ComboBox の ItemsSource をバインドするために、 を使用ElementNameしてバインドを という名前の UI オブジェクトにポイントし、SomePanel次に にバインドするように指示していDataContext.Statesます。これは、ItemsSource の最終バインディングが を指していることを意味しSomePanel.DataContext.Statesます。

于 2011-12-29T20:57:46.813 に答える
1

主張どおりに DataContext を正しく設定している場合はElementName、バインディングから を削除するだけです。これは、別の UIElement へのバインディングに使用されますClinicInfoRoot

于 2011-12-29T20:23:54.090 に答える