双向中AvalonEdit结合不起作用 [英] Two-way binding in AvalonEdit doesn't work
问题描述
我已经在我的项目是基于WPF和MVVM使用AvalonEdit。
$ B:阅读这篇文章我创建了下面的类后
$ b
公共类MvvmTextEditor:文本编辑,INotifyPropertyChanged的
{
公共静态的DependencyProperty DocumentTextProperty =
DependencyProperty.Register(DocumentText ,
typeof运算(字符串)的typeof(MvvmTextEditor),
新PropertyMetadata((OBJ,参数)=>
{
MvvmTextEditor目标=(MvvmTextEditor)目标文件;
target.DocumentText =(字符串)args.NewValue;
})
);
公共字符串DocumentText
{
{返回base.Text; }
集合{base.Text =价值; }
}
保护覆盖无效OnTextChanged(EventArgs的发送)
{
RaisePropertyChanged(DocumentText);
base.OnTextChanged(E);
}
公共事件PropertyChangedEventHandler的PropertyChanged;
公共无效RaisePropertyChanged(字符串信息)
{
如果(的PropertyChanged!= NULL)
{
的PropertyChanged(这一点,新PropertyChangedEventArgs(信息));
}
}
}
和使用下面的XAML来使用此控件:
< avalonedit:MvvmTextEditor X:NAME =xmlMessage>
< avalonedit:MvvmTextEditor.DocumentText>
<绑定路径=MessageXml模式=双向
UpdateSourceTrigger =的PropertyChanged>
< Binding.ValidationRules>
<局部:XMLMessageValidationRule />
< /Binding.ValidationRules>
< /绑定>
< / avalonedit:MvvmTextEditor.DocumentText>
< / avalonedit:MvvmTextEditor>
但结合作品单向
并没有按' T更新的我的字符串属性,也没有运行验证规则。
如何解决绑定按预期运行双向
<? / p>
WPF绑定不使用你的 DocumentText
财产;相反,他们直接访问依赖属性的基本价值。
您 OnTextChanged
方法实际上并没有更改值的基本依赖项属性。您将需要从 base.Text
的值复制到依赖属性上的每一个变化:
保护覆盖无效OnTextChanged(EventArgs的发送)
{
SetCurrentValue(DocumentTextProperty,base.Text);
base.OnTextChanged(E);
}
这问题会更容易,看看您是否遵循了正确的方式实施的DependencyProperty
:在 DocumentText
属性应该使用的GetValue
/ 的SetValue
方法,而不是访问一个不同的后备存储。
I have used AvalonEdit in my project that is based on WPF and MVVM. After reading this post I created the following class:
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));
}
}
}
and used the following XAML to use this control:
<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>
but the binding works OneWay
and doesn't update my string property nor run validation rule.
How can I fix binding to work as expected TwoWay
?
WPF bindings do not use your DocumentText
property; instead they access the underlying value of the dependency property directly.
Your OnTextChanged
method doesn't actually change the value of the underlying dependency property. You will need to copy the value from base.Text
into the dependency property on every change:
protected override void OnTextChanged(EventArgs e)
{
SetCurrentValue(DocumentTextProperty, base.Text);
base.OnTextChanged(e);
}
This issue would be easier to see if you followed the correct pattern for implementing DependencyProperty
: The DocumentText
property should use the GetValue
/SetValue
methods, and not access a different backing store.
这篇关于双向中AvalonEdit结合不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!