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?