WPF UserControl不继承父DataContext

时间:2017-10-27 09:55:04

标签: c# wpf xaml user-controls

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

代码如下。我期望的是我放在MainWindow.xaml上的TestUserControl的实例将继承DataContext,就像TextBlock一样。相反,它的DataContext似乎是null。有没有理由DataContext没有传递下去?我必须自动设置吗?

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

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

<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

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:  enter image description here

3 个答案:

答案 0 :(得分:2)

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

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

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

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

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

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

答案 1 :(得分:0)

尝试一下,您无需在绑定中使用任何RelativeSource:

<Grid Name="GrdContent">
    <TextBlock Name="TheTextBlock" Text="{Binding TextFromParent}" />

</Grid>

...

public MainWindow()
        {
            GrdContent.DataContext = this;

            InitializeComponent();

            SomeText = "New Text!";
        }

答案 2 :(得分:-1)

我已尝试实施上述解决方案(使用相同的代码),但无法使其正常工作。

MainWindow XAML 和代码隐藏:

<Grid>
    <StackPanel Orientation="Horizontal">
        <StackPanel Orientation="Vertical">
            <TextBlock Name="TestTextBlock1" Text="{Binding SomeText}" />
            <local:TestUserControl DataContext="{Binding SomeText}" />
            <TextBlock Name="TestTextBlock3" Text="{Binding SomeText}" />
        </StackPanel>
    </StackPanel>
</Grid>



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

    public MainWindow()
    {
        DataContext = this;

        InitializeComponent();

        SomeText = "Hello World!";
    }
    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));
    }
}

我的用户控制

<UserControl x:Class="ControlDataContext.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:ControlDataContext"
         mc:Ignorable="d" 
         d:DesignHeight="450" d:DesignWidth="800">
<Grid DataContext="{Binding RelativeSource={RelativeSource AncestorType=UserControl}}">
    <TextBlock Text="{Binding TextFromParent}" />
</Grid>

用户控制背后的代码

public partial class TestUserControl : UserControl
{
    public TestUserControl()
    {
        InitializeComponent();
    }
    public string TextFromParent
    {
        get => (string)GetValue(TextFromParentProperty);
        set => SetValue(TextFromParentProperty, value);
    }

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

用黄色高亮显示缺失线的图片

Result