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"; } }
}
}
Qustion是如何从MainWindow绑定到Children.DataContext.Text的?
**Text="{Binding ContentControl.Content.ContentControl.DataContext.Text}"**
它适用于Root控件DataContext
<TextBlock Text="{Binding ElementName=ContentControl,Path=Content.DataContext.Property}"/>
如何从MainWindow.ContentControl以Root UserControl的形式访问ContentControl.Content?
答案 0 :(得分:0)
通过添加属性儿童重新加载
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>