(WPF) Binding OneWayToSource with converter causes immediate exception

I have a TextBox in a window that I bind 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 appears as follows:

 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 delete either the converter or set the mode to TwoWay, an exception will not be thrown. Why otherwise an exception occurs, and how can I resolve or at least solve the problem?

Edit: It seems that you need to provide the data context in this scenario before you bind so that an exception does not occur. Why is this so?

+4
source share
2 answers

I believe that you get this error because you bind TextBox.TextProperty to nm, but TextBox.TextProperty is null. With two-way binding, you first need to send the value from nm to TextBox.TextProperty, setting it to "x" so that its value is no longer when it tries to link the opposite. Removing the converter probably also removes the check, which detects that TextBox.TextProperty is null and throws an exception.

So, if you have to add a line:

 oj.Fe.Text = "something"; 

Or perhaps even:

 oj.Fe.Text = string.Empty; 

front

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

then you should be fine.

EDIT: Actually, it was not an empty value, but a null sourceType that threw an exception.

I looked deeper with the decompiler, and it looks like you get the exception because sourceType is NULL. The IsValidValueForUpdate function, which throws a null reference, is only triggered when there is a converter, which explains why you do not get it when you remove the converter. The code was run during the backward conversion process, which explains why this happens with "OneWayToSource" as the binding mode. Regardless of the fact that this may be a minor error in the structure, setting up a datacontext before binding to provide sourceType seems like a good way.

+4
source

Can you do this? I believe the Binding constructor takes a string path, and you pass a string field. Thus, the compiler works fine, but the WPF mechanism gets confused. Again, I assume nm is a required source property that you want to get from the Target TextBox.

 Binding bnd = new Binding("nm"); 

I wrote this code and it worked.

 <Grid> <TextBox Name="fe"></TextBox> </Grid> // set datacontext Binding bnd = new Binding("nm"); bnd.Converter = new TestConverter(); bnd.Mode = BindingMode.OneWayToSource; fe.SetBinding(TextBox.TextProperty, bnd); fe.Text = "Hi"; public string nm { get { return _nm; } set { _nm = value; } } 
0
source

All Articles