I want to bind one data to several controls. Is there some logical control in WPF to achieve this?
For example i have a Grid
<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>
and I want to bind TextBlock to one data and Button and PlayerControl to another, like this:
<Container DataContext="{Binding Object2}">
<Button IsEnabled="{Binding IsSimulationRunning}" />
<controls:PlayerControl IsEnabled="{Binding IsLoaded}" />
</Container>
How can I do this in the best way?
The binding binds to the DataContext of the element containing the dependency property. And you can bind the DataContext to the underlying view model:
If your view model looks like this:
In this case you could bind to the objects directly. The main point is that you need to have the two objects in 1 common view model.