استفاده از AvalonEdit در WPF
نویسنده: وحید نصیری
تاریخ: ۱۳۹۳/۰۵/۲۷ ۱۲:۴۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
PM> install-package AvalonEdit
<Window x:Class="SyntaxHighlighter.MainWindow"
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"
Title="MainWindow" Height="401" Width="617">
<Grid>
<avalonEdit:TextEditor
Name="txtCode"
SyntaxHighlighting="C#"
FontFamily="Consolas"
FontSize="10pt"/>
</Grid>
</Window> using System;
using System.Collections.Concurrent;
using System.Reflection;
using System.Windows;
using System.Xml;
using ICSharpCode.AvalonEdit;
using ICSharpCode.AvalonEdit.Highlighting;
using ICSharpCode.AvalonEdit.Highlighting.Xshd;
namespace AvalonEditWpfTest.Controls
{
public class BindableAvalonTextEditor : TextEditor
{
public static readonly DependencyProperty BoundTextProperty =
DependencyProperty.Register("BoundText",
typeof(string),
typeof(BindableAvalonTextEditor),
new FrameworkPropertyMetadata(default(string), propertyChangedCallback));
public static string GetBoundText(DependencyObject obj)
{
return (string)obj.GetValue(BoundTextProperty);
}
public static void SetBoundText(DependencyObject obj, string value)
{
obj.SetValue(BoundTextProperty, value);
}
protected override void OnTextChanged(EventArgs e)
{
SetCurrentValue(BoundTextProperty, Text);
base.OnTextChanged(e);
}
private static void propertyChangedCallback(DependencyObject obj,
DependencyPropertyChangedEventArgs args)
{
var target = (BindableAvalonTextEditor)obj;
var value = args.NewValue;
if (value == null)
return;
if (string.IsNullOrWhiteSpace(target.Text) ||
!target.Text.Equals(args.NewValue.ToString()))
{
target.Text = args.NewValue.ToString();
}
}
}
} <Window x:Class="AvalonEditWpfTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:viewModels="clr-namespace:AvalonEditTests.ViewModels"
xmlns:controls="clr-namespace:AvalonEditWpfTest.Controls"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<viewModels:MainWindowViewModel x:Key="MainWindowViewModel"/>
</Window.Resources>
<Grid DataContext="{Binding Source={StaticResource MainWindowViewModel}}">
<controls:BindableAvalonTextEditor
BoundText="{Binding SourceCode, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
WordWrap="True"
ShowLineNumbers="True"
LineNumbersForeground="MediumSlateBlue"
FontFamily="Consolas"
VerticalScrollBarVisibility="Auto"
Margin="3"
HorizontalScrollBarVisibility="Auto"
FontSize="10pt"/>
</Grid>
</Window>
private static IHighlightingDefinition getHighlightingDefinition(string resourceName)
{
if (string.IsNullOrWhiteSpace(resourceName))
throw new NullReferenceException("Please specify SyntaxHighlightingResourceName.");
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
{
if (stream == null)
throw new NullReferenceException(string.Format("{0} resource is null.", resourceName));
using (var reader = new XmlTextReader(stream))
{
return HighlightingLoader.Load(reader, HighlightingManager.Instance);
}
}
} txtCode.SyntaxHighlighting = getHighlightingDefinition(resourceName);
using System;
using System.Collections.Concurrent;
using System.Reflection;
using System.Windows;
using System.Xml;
using ICSharpCode.AvalonEdit;
using ICSharpCode.AvalonEdit.Highlighting;
using ICSharpCode.AvalonEdit.Highlighting.Xshd;
namespace AvalonEditWpfTest.Controls
{
public class BindableAvalonTextEditor : TextEditor
{
public static readonly DependencyProperty SyntaxHighlightingResourceNameProperty =
DependencyProperty.Register("SyntaxHighlightingResourceName",
typeof(string),
typeof(BindableAvalonTextEditor),
new FrameworkPropertyMetadata(default(string), resourceNamePropertyChangedCallback));
public static string GetSyntaxHighlightingResourceName(DependencyObject obj)
{
return (string)obj.GetValue(SyntaxHighlightingResourceNameProperty);
}
public static void SetSyntaxHighlightingResourceName(DependencyObject obj, string value)
{
obj.SetValue(SyntaxHighlightingResourceNameProperty, value);
}
private static void loadHighlighter(TextEditor @this, string resourceName)
{
if (@this.SyntaxHighlighting != null)
return;
@this.SyntaxHighlighting = getHighlightingDefinition(resourceName);
}
private static void resourceNamePropertyChangedCallback(DependencyObject obj,
DependencyPropertyChangedEventArgs args)
{
var target = (BindableAvalonTextEditor)obj;
var value = args.NewValue;
if (value == null)
return;
loadHighlighter(target, value.ToString());
}
}
} <controls:BindableAvalonTextEditor SyntaxHighlightingResourceName = "AvalonEditWpfTest.Controls.sql-ce.xshd" />