WPF UserControl 不继承父 DataContext [英] WPF UserControl doesn't inherit parent DataContext

查看:45
本文介绍了WPF UserControl 不继承父 DataContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to develop a reusable UserControl but running into problems with binding. I've created a smaller application to test it but unable to sort it out, or at least understand why it's not working how I expect.

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

Code is below. What I would expect is the instance of the TestUserControl I put on MainWindow.xaml would inherit the DataContext there just like the TextBlock bellow it. Instead it's DataContext seems to be null. Is there a reason the DataContext doesn't pass down? Do I have to set it automatically?

主窗口.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>

MainWindow.xaml.cs

MainWindow.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));
        }
    }
}

TestUserControl.xaml

TestUserControl.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>

TestUserControl.xaml.cs

TestUserControl.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());
    }
}

程序在运行时如下所示,第一个文本是空白的,然后是具有工作绑定的 TextBlock:

Program looks like the following when run, first text is blank followed by TextBlock with working binding:

推荐答案

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

The UserControl is actually inheriting the DataContext from its parent element. There is however no TextFromParent property in that DataContext (because it is the MainWindow instance).

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

The Binding in the UserControl's XAML is supposed to bind to a property of the UserControl itself, not one of the current DataContext. Hence it must use the UserControl instance as source object:

<TextBlock Text="{Binding TextFromParent,
                  RelativeSource={RelativeSource AncestorType=UserControl}}" />

<小时>

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


Setting the UserControl's DataContext to itself is not an option, because it prevents that a DataContext value is inherited from the parent element of the control.

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

You may however set the DataContext of the root element in the UserControl's XAML to avoid setting RelativeSource on potentially many Bindings:

<UserControl ...>
    <Grid DataContext="{Binding RelativeSource={RelativeSource AncestorType=UserControl}}">
        <TextBlock Text="{Binding TextFromParent}" />
    </Grid>
</UserControl>

这篇关于WPF UserControl 不继承父 DataContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆