WPF UserControl 不继承父 DataContext

Wil*_* E. 1 c# wpf xaml user-controls

我正在尝试开发一个可重用的 UserControl,但遇到了绑定问题。我创建了一个较小的应用程序来测试它,但无法对其进行整理,或者至少理解为什么它没有按我的预期工作。

代码如下。我期望的是我放在 MainWindow.xaml 上的 TestUserControl 的实例将继承那里的 DataContext 就像下面的 TextBlock 一样。相反,它的 DataContext 似乎为空。DataContext 没有传递下去有什么原因吗?我必须自动设置吗?

主窗口.xaml

<Window x:Class="WpfTestApp.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:WpfTestApp"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel Orientation="Vertical">
        <local:TestUserControl TextFromParent="{Binding SomeText}" />
        <TextBlock Name="TestTextBlock" Text="{Binding SomeText}" />

    </StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)

主窗口.xaml.cs

using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;

namespace WpfTestApp
{
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        private string _someText;

        public MainWindow()
        {
            DataContext = this;

            InitializeComponent();

            SomeText = "New Text!";
        }

        public string SomeText
        {
            get { return _someText; }
            set
            {
                _someText = value;
                NotifyOnPropertyChanged();
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;

        protected void NotifyOnPropertyChanged([CallerMemberName] string propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

测试用户控件.xaml

<UserControl x:Class="WpfTestApp.TestUserControl"
             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:WpfTestApp"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <TextBlock Name="TheTextBlock" Text="{Binding TextFromParent}" />

    </Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)

测试用户控件.xaml.cs

using System.Windows;
using System.Windows.Controls;

namespace WpfTestApp
{
    public partial class TestUserControl : UserControl
    {
        public TestUserControl()
        {
            InitializeComponent();
        }

        public string TextFromParent
        {
            get { return (string)GetValue(TextFromParentProperty); }
            set { SetValue(TextFromParentProperty, value); }
        }

        public static readonly DependencyProperty TextFromParentProperty = DependencyProperty.Register(
            "TextFromParent", typeof(string), typeof(TestUserControl), new PropertyMetadata());
    }
}
Run Code Online (Sandbox Code Playgroud)

程序在运行时如下所示,第一个文本是空白的,然后是具有工作绑定的 TextBlock: 在此处输入图片说明

Cle*_*ens 5

UserControl 实际上是从其父元素继承 DataContext。但是TextFromParent,该 DataContext 中没有属性(因为它是 MainWindow 实例)。

UserControl 的 XAML 中的 Binding 应该绑定到 UserControl 本身的属性,而不是当前 DataContext 之一。因此它必须使用 UserControl 实例作为源对象:

<TextBlock Text="{Binding TextFromParent,
                  RelativeSource={RelativeSource AncestorType=UserControl}}" />
Run Code Online (Sandbox Code Playgroud)

将 UserControl 的 DataContext 设置为自身不是一个选项,因为它可以防止从控件的父元素继承 DataContext 值。

但是,您可以在 UserControl 的 XAML 中设置根元素的 DataContext 以避免在潜在的许多绑定上设置 RelativeSource:

<UserControl ...>
    <Grid DataContext="{Binding RelativeSource={RelativeSource AncestorType=UserControl}}">
        <TextBlock Text="{Binding TextFromParent}" />
    </Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)