C# WPF绑定成孙子
MainWindow.xamlC# WPF绑定成孙子,c#,wpf,binding,C#,Wpf,Binding,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
<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>
问题是如何从主窗口绑定到Children.DataContext.Text
**Text="{Binding ContentControl.Content.ContentControl.DataContext.Text}"**
它正在为根控件DataContext工作
<TextBlock Text="{Binding ElementName=ContentControl,Path=Content.DataContext.Property}"/>
如何从MainWindow.ContentControl以Root用户控件的身份访问ContentControl.Content?Resolved by added property Children
namespace grandson
{
/// <summary>
/// Interaction logic for Root.xaml
/// </summary>
public partial class Root : UserControl
{
public Root()
{
InitializeComponent();
}
public Control Children
{
get { return this.ContentControl; }
}
}
}
名称空间孙子
{
///
///Root.xaml的交互逻辑
///
公共部分类根:UserControl
{
公共根()
{
初始化组件();
}
公共控制儿童
{
获取{返回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>
<TextBlock Text="{Binding ElementName=ContentControl,Path=Content.DataContext.Property}"/>
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>