我在我的项目中使用了基于WPF和MVVM的AvalonEdit。 阅读this post后,我创建了以下类:AvalonEdit中的双向绑定不起作用
public class MvvmTextEditor : TextEditor, INotifyPropertyChanged
{
public static DependencyProperty DocumentTextProperty =
DependencyProperty.Register("DocumentText",
typeof(string), typeof(MvvmTextEditor),
new PropertyMetadata((obj, args) =>
{
MvvmTextEditor target = (MvvmTextEditor)obj;
target.DocumentText = (string)args.NewValue;
})
);
public string DocumentText
{
get { return base.Text; }
set { base.Text = value; }
}
protected override void OnTextChanged(EventArgs e)
{
RaisePropertyChanged("DocumentText");
base.OnTextChanged(e);
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
,并用下面的XAML使用此控件:
<avalonedit:MvvmTextEditor x:Name="xmlMessage">
<avalonedit:MvvmTextEditor.DocumentText>
<Binding Path ="MessageXml" Mode="TwoWay"
UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<local:XMLMessageValidationRule />
</Binding.ValidationRules>
</Binding>
</avalonedit:MvvmTextEditor.DocumentText>
</avalonedit:MvvmTextEditor>
但绑定工作OneWay
并且不更新我的字符串属性,也没有运行验证规则。
如何修复绑定按预期工作TwoWay
?
请问你能告诉我你是如何得到这个工作的?我问另一个问题[这里](http://stackoverflow.com/questions/18964176/two-way-binding-to-avalonedit-document-text-using-mvvm?noredirect=1#comment28010773_18964176)... – MoonKnight 2013-09-23 19:46:00