我试图使Avalon在我的WPF应用程序中兼容。通过谷歌搜索,我发现AvalonEdit对MVVM不友好和我需要导出AvalonEdit的状态,方法是创建一个从TextEditor派生的类,然后添加必要的依赖属性。恐怕我对格伦沃尔德先生的回答完全迷失了,这里
如果确实需要使用MVVM导出编辑器的状态,那么我建议您创建一个从TextEditor派生的类,该类添加必要的依赖属性,并将它们与AvalonEdit中的实际属性同步。
对于如何做到这一点,是否有人有榜样或有好的建议?
发布于 2012-09-13 05:11:03
Grunwald先生正在讨论用TextEditor包装依赖属性属性,以便您可以绑定到它们。基本思想如下(例如使用CaretOffset属性):
改进的TextEditor类
public class MvvmTextEditor : TextEditor, INotifyPropertyChanged
{
public static DependencyProperty CaretOffsetProperty =
DependencyProperty.Register("CaretOffset", typeof(int), typeof(MvvmTextEditor),
// binding changed callback: set value of underlying property
new PropertyMetadata((obj, args) =>
{
MvvmTextEditor target = (MvvmTextEditor)obj;
target.CaretOffset = (int)args.NewValue;
})
);
public new string Text
{
get { return base.Text; }
set { base.Text = value; }
}
public new int CaretOffset
{
get { return base.CaretOffset; }
set { base.CaretOffset = value; }
}
public int Length { get { return base.Text.Length; } }
protected override void OnTextChanged(EventArgs e)
{
RaisePropertyChanged("Length");
base.OnTextChanged(e);
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}既然CaretOffset已经包装在DependencyProperty中,就可以将其绑定到属性,比如视图模型中的Offset。作为说明,将Slider控件的值绑定到相同的视图模型属性Offset,并看到当您移动滑翔机时,将更新Avalon编辑器的光标位置:
测试XAML
<Window x:Class="AvalonDemo.TestWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:avalonEdit="http://icsharpcode.net/sharpdevelop/avalonedit"
xmlns:avalonExt="clr-namespace:WpfTest.AvalonExt"
DataContext="{Binding RelativeSource={RelativeSource Self},Path=ViewModel}">
<StackPanel>
<avalonExt:MvvmTextEditor Text="Hello World" CaretOffset="{Binding Offset}" x:Name="editor" />
<Slider Minimum="0" Maximum="{Binding ElementName=editor,Path=Length,Mode=OneWay}"
Value="{Binding Offset}" />
<TextBlock Text="{Binding Path=Offset,StringFormat='Caret Position is {0}'}" />
<TextBlock Text="{Binding Path=Length,ElementName=editor,StringFormat='Length is {0}'}" />
</StackPanel>
</Window>测试代码-隐藏
namespace AvalonDemo
{
public partial class TestWindow : Window
{
public AvalonTestModel ViewModel { get; set; }
public TestWindow()
{
ViewModel = new AvalonTestModel();
InitializeComponent();
}
}
}测试视图模型
public class AvalonTestModel : INotifyPropertyChanged
{
private int _offset;
public int Offset
{
get { return _offset; }
set
{
_offset = value;
RaisePropertyChanged("Offset");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}发布于 2012-09-12 20:08:53
您可以使用编辑器中的Document属性并将其绑定到ViewModel的属性。
以下是视图的代码:
<Window x:Class="AvalonEditIntegration.UI.View"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:AvalonEdit="clr-namespace:ICSharpCode.AvalonEdit;assembly=ICSharpCode.AvalonEdit"
Title="Window1"
WindowStartupLocation="CenterScreen"
Width="500"
Height="500">
<DockPanel>
<Button Content="Show code"
Command="{Binding ShowCode}"
Height="50"
DockPanel.Dock="Bottom" />
<AvalonEdit:TextEditor ShowLineNumbers="True"
Document="{Binding Path=Document}"
FontFamily="Consolas"
FontSize="10pt" />
</DockPanel>
</Window>以及ViewModel的代码:
namespace AvalonEditIntegration.UI
{
using System.Windows;
using System.Windows.Input;
using ICSharpCode.AvalonEdit.Document;
public class ViewModel
{
public ViewModel()
{
ShowCode = new DelegatingCommand(Show);
Document = new TextDocument();
}
public ICommand ShowCode { get; private set; }
public TextDocument Document { get; set; }
private void Show()
{
MessageBox.Show(Document.Text);
}
}
}来源:博客nawrem.reverse
发布于 2017-01-05 10:20:39
不确定这是否符合您的需要,但我找到了一种方法,可以访问TextEditor在ViewModel上的所有“重要”组件,同时将其显示在视图上,但仍在探索各种可能性。
我所做的不是在视图上实例化TextEditor,然后绑定我需要的许多属性,而是创建了一个Control,并将其内容绑定到我在ViewModel中创建的一个TextEditor实例。
视图:
<ContentControl Content="{Binding AvalonEditor}" />ViewModel:
using ICSharpCode.AvalonEdit;
using ICSharpCode.AvalonEdit.Document;
using ICSharpCode.AvalonEdit.Highlighting;
// ...
private TextEditor m_AvalonEditor = new TextEditor();
public TextEditor AvalonEditor => m_AvalonEditor;ViewModel (works!)中的测试代码
// tests with the main component
m_AvalonEditor.SyntaxHighlighting = HighlightingManager.Instance.GetDefinition("XML");
m_AvalonEditor.ShowLineNumbers = true;
m_AvalonEditor.Load(@"C:\testfile.xml");
// test with Options
m_AvalonEditor.Options.HighlightCurrentLine = true;
// test with Text Area
m_AvalonEditor.TextArea.Opacity = 0.5;
// test with Document
m_AvalonEditor.Document.Text += "bla";目前,我仍然在确切地决定我的应用程序需要用textEditor配置/做什么,但是从这些测试中,我似乎可以在保持MVVM方法的同时更改它的任何属性。
https://stackoverflow.com/questions/12344367
复制相似问题