次の簡単なコンバーターで値にバインドしているウィンドウに TextBox があります。
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";
}
}
バインディング自体は次のように明示されています。
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
コンバーターを削除するか、モードを TwoWay に設定しても、例外は発生しません。それ以外の場合に例外が発生するのはなぜですか? また、問題を解決または少なくとも回避するにはどうすればよいですか?
編集:例外が発生しないようにバインドする前に、このシナリオではデータ コンテキストを提供する必要があるようです。なぜそうなのですか?