gpt4 book ai didi

c# - 使用 MVVM 绑定(bind)到 AvalonEdit 文档文本的两种方式

转载 作者:IT王子 更新时间:2023-10-29 04:15:28 26 4
gpt4 key购买 nike

我想将 AvalonEdit TextEditor 控件包含到我的 MVVM 应用程序中。我需要的第一件事是能够绑定(bind)到 TextEditor.Text 属性,以便我可以显示文本。为此,我遵循了 Making AvalonEdit MVVM compatible 中给出的示例。 .现在,我已经使用接受的答案作为模板实现了以下类

public sealed class MvvmTextEditor : TextEditor, INotifyPropertyChanged
{
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(MvvmTextEditor),
new PropertyMetadata((obj, args) =>
{
MvvmTextEditor target = (MvvmTextEditor)obj;
target.Text = (string)args.NewValue;
})
);

public new string Text
{
get { return base.Text; }
set { base.Text = value; }
}

protected override void OnTextChanged(EventArgs e)
{
RaisePropertyChanged("Text");
base.OnTextChanged(e);
}

public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string info)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}

XAML 在哪里

<Controls:MvvmTextEditor HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
FontFamily="Consolas"
FontSize="9pt"
Margin="2,2"
Text="{Binding Text, NotifyOnSourceUpdated=True, Mode=TwoWay}"/>

首先,这是行不通的。 Binding 根本没有显示在 Snoop 中(不是红色,什么都没有,事实上我什至看不到 Text 依赖属性)。

我看到这个问题和我的一模一样Two-way binding in AvalonEdit doesn't work但接受的答案 有效(至少对我而言)。所以我的问题是:

如何使用上述方法执行双向绑定(bind)以及我的 MvvmTextEditor 类的正确实现是什么?

感谢您的宝贵时间。


注意:我的 ViewModel 中有我的 Text 属性,它实现了所需的 INotifyPropertyChanged 接口(interface)。

最佳答案

创建一个将附加 TextChanged 事件的行为类,并将连接绑定(bind)到 ViewModel 的依赖属性。

AvalonTextBehavior.cs

public sealed class AvalonEditBehaviour : Behavior<TextEditor> 
{
public static readonly DependencyProperty GiveMeTheTextProperty =
DependencyProperty.Register("GiveMeTheText", typeof(string), typeof(AvalonEditBehaviour),
new FrameworkPropertyMetadata(default(string), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, PropertyChangedCallback));

public string GiveMeTheText
{
get { return (string)GetValue(GiveMeTheTextProperty); }
set { SetValue(GiveMeTheTextProperty, value); }
}

protected override void OnAttached()
{
base.OnAttached();
if (AssociatedObject != null)
AssociatedObject.TextChanged += AssociatedObjectOnTextChanged;
}

protected override void OnDetaching()
{
base.OnDetaching();
if (AssociatedObject != null)
AssociatedObject.TextChanged -= AssociatedObjectOnTextChanged;
}

private void AssociatedObjectOnTextChanged(object sender, EventArgs eventArgs)
{
var textEditor = sender as TextEditor;
if (textEditor != null)
{
if (textEditor.Document != null)
GiveMeTheText = textEditor.Document.Text;
}
}

private static void PropertyChangedCallback(
DependencyObject dependencyObject,
DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
var behavior = dependencyObject as AvalonEditBehaviour;
if (behavior.AssociatedObject!= null)
{
var editor = behavior.AssociatedObject as TextEditor;
if (editor.Document != null)
{
var caretOffset = editor.CaretOffset;
editor.Document.Text = dependencyPropertyChangedEventArgs.NewValue.ToString();
editor.CaretOffset = caretOffset;
}
}
}
}

View.xaml

 <avalonedit:TextEditor
WordWrap="True"
ShowLineNumbers="True"
LineNumbersForeground="Magenta"
x:Name="textEditor"
FontFamily="Consolas"
SyntaxHighlighting="XML"
FontSize="10pt">
<i:Interaction.Behaviors>
<controls:AvalonEditBehaviour GiveMeTheText="{Binding Test, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
</i:Interaction.Behaviors>
</avalonedit:TextEditor>

i 必须定义为

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

ViewModel.cs

    private string _test;
public string Test
{
get { return _test; }
set { _test = value; }
}

这应该会为您提供文本并将其推送回 ViewModel。

关于c# - 使用 MVVM 绑定(bind)到 AvalonEdit 文档文本的两种方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18964176/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com