我正在尝试开发一个可重用的UserControl,但在绑定方面遇到了问题。我已经创建了一个较小的应用程序来测试它,但是无法对它进行分类,或者至少理解它为什么不能以我所期望的方式工作。
密码在下面。我所期望的是,我在TestUserControl上安装的MainWindow.xaml的实例将继承DataContext,就像TextBlock对它的咆哮一样。相反,它的DataContext似乎是空的。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:

发布于 2017-10-27 09:58:32
UserControl实际上是从其父元素继承DataContext。但是,该TextFromParent中没有DataContext属性(因为它是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>发布于 2019-11-24 10:31:09
尝试这样做,您就不需要在绑定中使用任何RelativeSource:
<Grid Name="GrdContent">
<TextBlock Name="TheTextBlock" Text="{Binding TextFromParent}" />
</Grid>..。
public MainWindow()
{
GrdContent.DataContext = this;
InitializeComponent();
SomeText = "New Text!";
}发布于 2021-06-10 13:25:07
我试图实现上述解决方案(使用相同的代码),但无法使其工作。
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());
}用黄色高亮显示缺失线的图片

https://stackoverflow.com/questions/46972319
复制相似问题