AvalonEdit Change Syntax Highlighting in Code

Issue

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?

Solution

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);

Answered By – Jeremy Thompson

Answer Checked By – Mildred Charles (AngularFixing Admin)

Leave a Reply

Your email address will not be published.