(WPF)将OneWayToSource与转换器绑定会导致即时异常 [英] (WPF) Binding OneWayToSource with a converter results in immediate exception

查看:177
本文介绍了(WPF)将OneWayToSource与转换器绑定会导致即时异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 公开的

我在一个窗口中有一个TextBox, class TestConverter:MarkupExtension,IValueConverter {
public override object ProvideValue(IServiceProvider serviceProvider){
return this;
}

public object Convert(object value,Type targetType,object parameter,CultureInfo culture){
returnx;
}

public object ConvertBack(object value,Type targetType,object parameter,CultureInfo culture){
returny;
}
}

绑定本身显示如下:

 绑定bnd =新绑定(nm); //'nm'是一个字符串,其绑定路径只是
//未来源对象的属性名称
bnd.Converter = new TestConverter();
bnd.Mode = BindingMode.OneWayToSource;
oj.Fe.SetBinding(TextBox.TextProperty,bnd); //< --- ---异常发生在这里

如果我删除转换器或将模式设置为TwoWay也不例外。为什么会出现异常,如何解决或至少解决问题?



编辑:似乎有一个必须在绑定之前,在这种情况下提供数据上下文,不会引起异常。为什么是这样?

解决方案

我相信你会得到这个错误,因为你将TextBox.TextProperty绑定到nm,但是TextBox.TextProperty为null。使用双向绑定,它必须首先将值从nm发送到TextBox.TextProperty,将其设置为x,以使其在尝试以其他方式绑定时不再为空。删除转换器也可能会删除发现TextBox.TextProperty为null并生成异常的检查。



所以,如果你要添加一行:

  oj.Fe.Text =something; 

甚至可能:

  oj.Fe.Text = string.Empty; 

before

  oj.Fe.SetBinding(TextBox.TextProperty,bnd); 

那么你应该可以。



编辑:
实际上它不是一个空值,而是引起异常的null sourceType。



我用一个反编译器看起来更深,看起来像得到的异常是因为sourceType为null。导致空引用异常的IsValidValueForUpdate函数仅在有转换器时运行,这就解释了为什么在卸下转换器时不会得到它。代码在转换过程中运行,这解释了为什么OneWayToSource作为绑定模式发生。无论如何,它可能只是框架中的一个小错误,所以在绑定之前设置datacontext以提供sourceType似乎是一个很好的解决方法。


I've a TextBox in a window which I'm binding to a value with the following trivial converter:

public class TestConverter : MarkupExtension, IValueConverter {
    public override object ProvideValue(IServiceProvider serviceProvider) {
        return this;
    }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
        return "x";
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
        return "y";
    }
}

The binding itself is manifesting like so:

Binding bnd = new Binding(nm); // 'nm' is a string with the binding path which is just
                               // a property name of the future source object
bnd.Converter = new TestConverter();
bnd.Mode = BindingMode.OneWayToSource;
oj.Fe.SetBinding(TextBox.TextProperty, bnd); // <--- Exception occurs here

If I remove either the converter or set the mode to TwoWay no exception is raised. Why is an exception being raised otherwise, and how can I resolve or at least work around the issue?

Edit: It seems one has to provide a data context in this scenario before binding for there to be no exception raised. Why is it so?

解决方案

I believe you are getting that error because you are binding the TextBox.TextProperty to nm, but the TextBox.TextProperty is null. With a two way binding it must send the value from nm to the TextBox.TextProperty first, setting it to "x", so that its not null anymore when it tries to bind back the other way. Removing the converter probably also removes the check that discovers that the TextBox.TextProperty is null and produces the exception.

So, if you were to add the line:

oj.Fe.Text = "something";

Or possibly even:

oj.Fe.Text = string.Empty;

before

oj.Fe.SetBinding(TextBox.TextProperty, bnd);

then you should be ok.

EDIT: Actually it wasn't a null value but a null sourceType that caused the exception.

I looked in deeper with a decompiler and it looks like the exception you get is because the sourceType is null. The "IsValidValueForUpdate" function which causes the null reference exception only runs when there is a converter, which explains why you dont get it when you remove the converter. The code was run in the process of converting back, which explains why it happens with "OneWayToSource" as the binding mode. Regardless, it may just be a minor bug in the framework, so setting the datacontext before binding to provide the sourceType seems to be a good workaround.

这篇关于(WPF)将OneWayToSource与转换器绑定会导致即时异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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