WPF internal control binding with parent data context

I made user control

<UserControl x:Class="MyApp.MyControl"
         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" 
         mc:Ignorable="d"  x:Name="uc">
<Grid Width="Auto" Height="Auto">
    <TextBlock Text="{Binding Path=DataContext.TextContent, ElementName=uc}"/>
    <TextBlock Text="{Binding Path=DataContext.TextContent2, ElementName=uc}"/>
</Grid>

I want sub-controls in a specific control (uc) to bind to the uc.DataContext properties. I used a specific control as follows:

<Window x:Class="Tms.TMSClient.Views.MainWindow" Name="window"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:control="clr-namespace:MyApp"
    xmlns:ribbon="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary">      

    <control:MyControl DataContext="{Binding Path=MyControlVM}"/>

The DataContext that is assigned to the window has the following structure: WindowVM.MyControlVM.TextContent.

This code does not work because the DataContext text field is bound to WindowVM. I think the problem may be due to the fact that the inner text block is linked before a certain control (uc), so the limited DataContext for uc has not yet taken effect.

: (MyControl) (MyControlVM), MyControl MyControlVM.

?

+4
3
<TextBlock Text="{Binding Path=TextContent}"/>

.

MainWindow.xaml

<Window x:Class="DataContextTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:my="clr-namespace:DataContextTest"
    Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
    <my:MyOuterDataContext />
</Window.DataContext>
<Grid>
    <my:MyControl DataContext="{Binding Path=MyInnerDataContext}" />
</Grid>

MyControl.xaml

<UserControl x:Class="DataContextTest.MyControl"
         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" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid>
    <TextBlock Text="{Binding Path=TextContent}" />
</Grid>

DataContexts:

public class MyOuterDataContext
{
    public MyInnerDataContext MyInnerDataContext { get; set; }

    public MyOuterDataContext()
    {
        MyInnerDataContext = new MyInnerDataContext();
    }
}

public class MyInnerDataContext
{
    public string TextContent { get { return "foo"; } }
}
+2

, , MyControl TextBox.Text MyControl UserControl. , RelativeSource Binding ElementName, .

, ​​ DataContext UserControl:

public MyControl()
{
    DataContext = new YourControlViewModel();
}

DataContext, TextBox MyControl.DataContext UserControl XAML:

<TextBlock Text="{Binding DataContext.TextContent, 
    RelativeSource={RelativeSource AncestorType={x:Type Window}}}" />

, .

+5

DataContext . , .

, DataContext , :

<control:MyControl DataContext="{Binding Path=TextContent}"/>
+4

All Articles