AvalonEdit Change Syntax Highlighting in Code
Asked Answered
N

5

12

I want to change the Syntax Highlighting of AvalonEdit in my Code.

XAML:

 <avalonEdit:TextEditor Name="textEditor" SyntaxHighlighting="{Binding syntaxHighlighting}" />

C#:

public string syntaxHighlighting { get; set; }

public MainWindow()
{
     InitializeComponent();
     syntaxHighlighting = "C#";
     DataContext = this;
}

But the Syntax Highlighting is not changed. What am I doing wrong? Is there a better solution for my problem?

Nation answered 23/4, 2013 at 12:39 Comment(0)
M
8

Here you go:

ICSharpCode.AvalonEdit.TextEditor textEditor = new ICSharpCode.AvalonEdit.TextEditor();
textEditor.ShowLineNumbers = true;
string dir = @"C:\Program Files\MyFolder\";
#if DEBUG
dir = @"C:\Dev\Sandbox\SharpDevelop-master\src\Libraries\AvalonEdit\ICSharpCode.AvalonEdit\Highlighting\Resources\";
#endif

Stream xshd_stream = File.OpenRead(dir + "CSharp-Mode.xshd");
XmlTextReader xshd_reader = new XmlTextReader(xshd_stream);
textEditor.SyntaxHighlighting = ICSharpCode.AvalonEdit.Highlighting.Xshd.HighlightingLoader.Load(xshd_reader, ICSharpCode.AvalonEdit.Highlighting.HighlightingManager.Instance);
xshd_reader.Close();
xshd_stream.Close();

Edit:

Since ICSharp.TextEditor throws AccessViolations in WinForms, I use AvalonEdit in WinForms:

ElementHost host = new ElementHost();
host.Size = new Size(200, 100);
host.Location = new Point(100, 100);
host.Child = textEditor;
this.Controls.Add(host);
Mastigophoran answered 6/5, 2013 at 5:17 Comment(0)
S
36
ICSharpCode.AvalonEdit.Highlighting.HighlightingManager.Instance.GetDefinition("C#");
Surplus answered 13/6, 2013 at 15:20 Comment(2)
ICSharpCode.AvalonEdit.Highlighting.HighlightingManager.Instance.GetDefinitionByExtension(Path.GetExtension(FileName));Basket
Great answer. Never thought that it's hidden so deeeeeeeep, thanks all.Passive
M
8

Here you go:

ICSharpCode.AvalonEdit.TextEditor textEditor = new ICSharpCode.AvalonEdit.TextEditor();
textEditor.ShowLineNumbers = true;
string dir = @"C:\Program Files\MyFolder\";
#if DEBUG
dir = @"C:\Dev\Sandbox\SharpDevelop-master\src\Libraries\AvalonEdit\ICSharpCode.AvalonEdit\Highlighting\Resources\";
#endif

Stream xshd_stream = File.OpenRead(dir + "CSharp-Mode.xshd");
XmlTextReader xshd_reader = new XmlTextReader(xshd_stream);
textEditor.SyntaxHighlighting = ICSharpCode.AvalonEdit.Highlighting.Xshd.HighlightingLoader.Load(xshd_reader, ICSharpCode.AvalonEdit.Highlighting.HighlightingManager.Instance);
xshd_reader.Close();
xshd_stream.Close();

Edit:

Since ICSharp.TextEditor throws AccessViolations in WinForms, I use AvalonEdit in WinForms:

ElementHost host = new ElementHost();
host.Size = new Size(200, 100);
host.Location = new Point(100, 100);
host.Child = textEditor;
this.Controls.Add(host);
Mastigophoran answered 6/5, 2013 at 5:17 Comment(0)
F
3

If you want to bind to a string, you can define an IValueConverter to wrap the built in HighlightingDefinitionTypeConverter:

using System;
using System.Globalization;
using System.Windows.Data;
using ICSharpCode.AvalonEdit.Highlighting;

public class HighlightingDefinitionConverter : IValueConverter
{
    private static readonly HighlightingDefinitionTypeConverter Converter = new HighlightingDefinitionTypeConverter();

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return Converter.ConvertFrom(value);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return Converter.ConvertToString(value);
    }
}

Then, just add the converter to your binding:

<avalonEdit:TextEditor Name="textEditor" SyntaxHighlighting="{Binding syntaxHighlighting, Converter={StaticResource HighlightingDefinitionConverter}}" />
Featheredge answered 24/7, 2015 at 16:1 Comment(1)
Perfect for MVVM!Carrington
C
1

Just use TypeConverter

var typeConverter = new HighlightingDefinitionTypeConverter();
var xmlSyntaxHighlighter = (IHighlightingDefinition)typeConverter.ConvertFrom("XML");
var csSyntaxHighlighter = (IHighlightingDefinition)typeConverter.ConvertFrom("C#");
CSharpEditor.SyntaxHighlighting = csSyntaxHighlighter;
XmlEditor.SyntaxHighlighting = xmlSyntaxHighlighter;
Complete answered 26/2, 2014 at 10:28 Comment(0)
E
0

syntaxHighlighting is not a string! your code might not compile! Note that SyntaxHighlighting in XAML here uses markup extensions, which instantiates a instance of SyntaxHighlighting, not a string.

private void OnTabLoaded(object sender, RoutedEventArgs e)
{
   textEditor.SyntaxHighlighting = HighlightingLoader.Load(..., HighlightingManager.Instance);
   textEditor.SyntaxHighlighting.MainRuleSet=...
}

Go to sharpdevelop and download the source code.

Entomo answered 23/4, 2013 at 12:48 Comment(2)
Oh I see it, mh do you know how i can solve it? Can i change the Syntax when I have only the Name of it?Nation
You'd better download SharpDevelop source and have a study on the source, it will help much.Entomo

© 2022 - 2024 — McMap. All rights reserved.