MainWindow.xaml
<Window x:Class="grandson.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:grandson" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Window.Resources> <local:Root x:Key="RootControl"/> </Window.Resources> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition/> </Grid.ColumnDefinitions> <TextBlock Text="{Binding ContentControl.Content.ContentControl.DataContext.Text}"/> <ContentControl Name="ContentControl" Grid.Column="1" Content="{StaticResource RootControl}" /> </Grid> </Window>
Root.xaml
<UserControl x:Class="grandson.Root" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:grandson" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800" Background="Red"> <UserControl.Resources> <local:Children x:Key="Children"/> </UserControl.Resources> <UserControl.DataContext> <local:RootViewModel/> </UserControl.DataContext> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition/> </Grid.ColumnDefinitions> <ContentControl Name="ContenControl" Grid.Column="1" Content="{StaticResource Children}"/> <Label Content="Root" FontSize="72" Grid.RowSpan="2" HorizontalContentAlignment="Center" /> </Grid> </UserControl>
Children.xaml
<UserControl x:Class="grandson.Children" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:grandson" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800" Background="Yellow"> <UserControl.DataContext> <local:ChildrenViewModel/> </UserControl.DataContext> <Grid> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition/> </Grid.RowDefinitions> <Label Content="Children" FontSize="72" Grid.RowSpan="2" Grid.ColumnSpan="2" HorizontalContentAlignment="Center" /> <Label Grid.Row="1" FontSize="72" Content="{Binding Text}"/> </Grid> </UserControl>
ChildrenViewModel.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace grandson { public class ChildrenViewModel { public ChildrenViewModel() { } public string Text { get { return "asdf"; } } } }
Quration, как я могу привязаться к Children.DataContext.Text из MainWindow?
**Text="{Binding ContentControl.Content.ContentControl.DataContext.Text}"**
Работает на Root control DataContext
<TextBlock Text="{Binding ElementName=ContentControl,Path=Content.DataContext.Property}"/>
Как получить доступ к ContentControl.Content из MainWindow.ContentControl как Root UserControl?
Решено добавленной собственностью Дети
namespace grandson { /// <summary> /// Interaction logic for Root.xaml /// </summary> public partial class Root : UserControl { public Root() { InitializeComponent(); } public Control Children { get { return this.ContentControl; } } } }
И так связать:
<Window x:Class="grandson.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:grandson" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Window.Resources> <local:Root x:Key="RootControl"/> </Window.Resources> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition/> </Grid.ColumnDefinitions> <TextBlock Text="{Binding Path=Content.Children.Content.DataContext.Text, ElementName=ContentControl}"/> <ContentControl Name="ContentControl" Grid.Column="1" Content="{StaticResource RootControl}" /> </Grid> </Window>