我正在尝试开发可重用的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());
}
}
答案 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)
<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());
}