Reputation: 1332
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?
Upvotes: 12
Views: 14311
Reputation: 65564
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);
Upvotes: 8
Reputation: 4386
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}}" />
Upvotes: 4
Reputation: 261
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;
Upvotes: 1
Reputation: 405
ICSharpCode.AvalonEdit.Highlighting.HighlightingManager.Instance.GetDefinition("C#");
Upvotes: 36
Reputation: 16277
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.
Upvotes: 0