调试/断点WPF BindingExpression为空的原因 [英] Debug/Breakpoint why a WPF BindingExpression is null

查看:224
本文介绍了调试/断点WPF BindingExpression为空的原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF CustomControl,它具有一个名为SelectedRange的依赖属性。

I have a WPF CustomControl with a depdendency property called SelectedRange.

在XAML应用程序中,绑定设置如下:

In an application in XAML the binding is set like this:

SelectedRange="{Binding Source={StaticResource ResourceKey=MainWindow},
                         Path=GlobalXRange, Mode=TwoWay}"

我在代码中发现BindingExpression被覆盖(设置为null)。我可以使用以下代码确定它:

I find in the code that the BindingExpression is getting overwritten (set to null). I can determine this with this code:

public IRange SelectedRange
{
    get { return (IRange)GetValue(SelectedRangeProperty); }
    set
    {
        SetValue(SelectedRangeProperty, value);
        Binding b = BindingOperations.GetBinding(this, SelectedRangeProperty);

        BindingExpression be =
             BindingOperations.GetBindingExpression(this, SelectedRangeProperty);
        if (be == null)
        {
            Console.WriteLine("Binding expression is null!");
        }
    }
}

事实证明BindingExpression是应用程序启动后为null。

It turns out the BindingExpression is null after the application starts.

所以我的问题是-如何调试为什么BindingExpression为null-例如它是在XAML中设置的,如何找出设置为null的位置呢?

So my question is - how can I debug why this BindingExpression is null - e.g. it is set in XAML how can I find out where it is being set to null?

注意:输出控制台中没有绑定错误

Note: There are no binding errors in the output console

推荐答案

好,谢谢您,我设法解决了所有问题。对于未来的读者,这是我的做法。

Ok guys, thanks to all your help I managed to solve this. For future readers here's how I did it.

此问题是由 DependencyProperty优先级。绑定到SelectedRange的TwoWay绑定位于ItemTemplate内部,并且被调用SetValue(SelectedRangeProperty,value)的自定义控件覆盖。

This problem was caused by DependencyProperty Precedence. The TwoWay binding to SelectedRange was inside an ItemTemplate and was being overwritten by the Custom Control which was calling SetValue(SelectedRangeProperty, value).

WPF中的解决方案是使用 SetCurrentValue(SelectedRangeProperty,值)

The solution in WPF is to use SetCurrentValue(SelectedRangeProperty, value).

但是,我正在编写一个跨平台控件(Silverlight,WinRT),而SL / WinRT中没有此方法。要解决此问题,我必须创建一个绑定以通过代码中的代理来设置SelectedRangeProperty,例如

However, I am writing a cross-platform control (Silverlight, WinRT) and this method is not available in SL/WinRT. To workaround I had to create a binding to set the SelectedRangeProperty via a proxy in code, e.g.

var binding = new Binding();
binding.Source = this;
binding.Path = new PropertyPath(SelectedRangeProxyProperty);
binding.Mode = BindingMode.TwoWay;
this.SetBinding(SelectedRangeProperty, binding);

感谢您的所有帮助,希望对您有所帮助!

Thank you for all your help, hope this helps someone else!

这篇关于调试/断点WPF BindingExpression为空的原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆