2012-04-07 5 views
0

1つのデータを複数のコントロールにバインドしたい。これを実現するには、いくつかの論理的なコントロールがWPFにありますか?例えば iはGridデータをバインドするためのWPFの論理コンテナ

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto" /> 
     <RowDefinition Height="*" /> 
    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition /> 
     <ColumnDefinition /> 
    </Grid.ColumnDefinitions> 

    <TextBlock Text="{Binding Name}" /> 
    <Button Grid.Column="1" Grid.RowSpan="2" IsEnabled="{Binding IsSimulationRunning}" /> 
    <controls:PlayerControl Grid.Row="1" IsEnabled="{Binding IsLoaded}" /> 
</Grid> 

持っていると私はこのような別のデータにTextBlockButtonPlayerControlをバインドする:

<Container DataContext="{Binding Object2}"> 
    <Button IsEnabled="{Binding IsSimulationRunning}" /> 
    <controls:PlayerControl IsEnabled="{Binding IsLoaded}" /> 
</Container> 

がどのように私は最善の方法でこれを行うことができますか?

答えて

1

バインディングは、依存関係プロパティを含む要素のDataContextにバインドされます。そして、あなたは、基礎となるビューモデルにのDataContextをバインドすることができます。

<TextBlock DataContext="{Binding Object1}" Text="{Binding Name}" /> 
<Button Grid.Column="1" Grid.RowSpan="2" 
    DataContext="{Binding Object2}" IsEnabled="{Binding IsSimulationRunning}" /> 
<controls:PlayerControl Grid.Row="1" 
    DataContext="{Binding Object2}" IsEnabled="{Binding IsLoaded}" /> 

あなたのビューモデルは次のようになります場合:あなたは直接オブジェクトにバインドすることができ、この場合には

public class PlayerViewModel {   
    public TrackViewModel Object1 { get; set; } 
    public PlaybackViewModel Object2 { get; set; } 
} 
public class TrackViewModel { public string Name { get; set; } } 
public class PlaybackViewModel { 
    public bool IsLoaded { get; set; } 
    public bool IsSimulationRunning { get; set; } 
} 

。要点は、1つの共通ビューモデルで2つのオブジェクトを持つ必要があることです。

<TextBlock Text="{Binding Path=Object1.Name}" /> 
<Button Grid.Column="1" Grid.RowSpan="2" IsEnabled="{Binding Path=Object2.IsSimulationRunning}" /> 
<controls:PlayerControl Grid.Row="1" IsEnabled="{Binding Path=Object2.IsLoaded}" /> 
+0

DataContext = "{Binding Object2}"(投稿の変更点を参照してください)の2回以上の文を書くことはできません。 –

関連する問題