WPF 用户控件的 DataContext 为 Null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/296503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:48:09  来源:igfitidea点击:

WPF User Control's DataContext is Null

wpfwpf-controls

提问by Justin Bozonier

I have a user control where the XAML of the control can bind to the appropriate properties from the parent's data context like normal (the data context propagates in xaml).

我有一个用户控件,其中控件的 XAML 可以像正常一样绑定到来自父级数据上下文的适当属性(数据上下文在 xaml 中传播)。

For example, I have a window whose DataContext I am setting to ObjectA for example. My user control within the window is then try to access the properties within the dataContext

例如,我有一个窗口,例如我将其 DataContext 设置为 ObjectA。我在窗口中的用户控件然后尝试访问 dataContext 中的属性

So my window's xaml and code behind can both see a non-null DataContext.

所以我的窗口的 xaml 和后面的代码都可以看到一个非空的 DataContext。

My control that DataContext propagates to can see a non-null DataContext in the Xaml but not in the code behind.

我的 DataContext 传播到的控件可以在 Xaml 中看到一个非空的 DataContext,但在后面的代码中看不到。

What is the proper way of handling this?

处理这个问题的正确方法是什么?

回答by Bijington

failing that if you need to check whether the DataContext is being set you can use the DataContextChanged

如果您需要检查是否正在设置 DataContext 失败,则可以使用 DataContextChanged

public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();

        DataContextChanged += new DependencyPropertyChangedEventHandler(UserControl1_DataContextChanged);
    }

    void UserControl1_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        // You can also validate the data going into the DataContext using the event args
    }
}

Note it wont enter UserControl1_DataContextChanged until DataContext is changed from null to a different value.

请注意,它不会进入 UserControl1_DataContextChanged,直到 DataContext 从 null 更改为不同的值。

Not sure if this answers your question but can be quite handy to use in debugging issues.

不确定这是否能回答您的问题,但在调试问题时非常方便。

回答by Jobi Joy

I think you are checking the 'DataContext' in the constructor of the UserControl. It will be null at the Constructor since the user control hasnt yet created while execution is in the constructor code. But check the property at Loaded event you will see the object properly.

我认为您正在检查 UserControl 的构造函数中的“DataContext”。由于在构造函数代码中执行时尚未创建用户控件,因此在构造函数中它将为空。但是检查 Loaded 事件中的属性,您将正确地看到该对象。

public partial class UserControl1
{
    public UserControl1()
    {
        this.InitializeComponent();

        //DataContext will be null here 
        this.Loaded += new RoutedEventHandler(UserControl1_Loaded);
    }

    void UserControl1_Loaded(object sender, RoutedEventArgs e)
    {
        //Check DataContext Property here - Value is not null
    }
}

回答by hughdbrown

I would check to see whether you are having a binding error at runtime. Add this namespace to your XAML:

我会检查您是否在运行时遇到绑定错误。将此命名空间添加到您的 XAML:

xmlns:debug="clr-namespace:System.Diagnostics;assembly=System"

and check the debugger's Output window for relevant error messages.

并检查调试器的输出窗口以获取相关错误消息。

Alternatively, can you show us more code?

或者,您能否向我们展示更多代码?