Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF UserControl不继承父数据上下文_C#_Wpf_Xaml_User Controls - Fatal编程技术网

C# WPF UserControl不继承父数据上下文

C# WPF UserControl不继承父数据上下文,c#,wpf,xaml,user-controls,C#,Wpf,Xaml,User Controls,我正在尝试开发一个可重用的UserControl,但在绑定方面遇到了问题。我已经创建了一个较小的应用程序来测试它,但无法对其进行分类,或者至少无法理解它为什么不能按我的预期工作 代码如下。我所期望的是我放在MainWindow.xaml上的TestUserControl实例将继承那里的DataContext,就像下面的TextBlock一样。相反,它的DataContext似乎为空。DataContext没有传递下去有什么原因吗?我必须自动设置吗 MainWindow.xaml <Wind

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

代码如下。我所期望的是我放在MainWindow.xaml上的TestUserControl实例将继承那里的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>
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>
程序运行时如下所示,第一个文本为空,后面是具有工作绑定的TextBlock:

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>

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>

试试这个,您不需要在绑定中使用任何RelativeSource:

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

</Grid>

试试这个,您不需要在绑定中使用任何RelativeSource:

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

</Grid>
我曾尝试使用相同的代码实现上述解决方案,但无法使其正常工作。 主窗口XAML和代码隐藏: 用黄色突出显示缺少的线的图片 我曾尝试使用相同的代码实现上述解决方案,但无法使其正常工作。 主窗口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());
}