Search code examples
c#wpfwinformsavalonediticsharpcode

How can I reset ICSharpCode.AvalonEdit syntax highlighting?


I am hosting the ICSharpCode AvalonEdit source-code editing WPF control into my Windows Forms C# application. I know that the following code loads a syntax highlighting definition:

ICSharpCode.AvalonEdit.TextEditor MyEditor = new ICSharpCode.AvalonEdit.TextEditor();
MyEditor.ShowLineNumbers = true;

Stream xshd_stream = File.OpenRead("C:\\Syntax Highlighting\\php.xsdh");
XmlTextReader xshd_reader = new XmlTextReader(xshd_stream);

// Apply the new syntax highlighting definition.
MyEditor.SyntaxHighlighting = ICSharpCode.AvalonEdit.Highlighting.Xshd.HighlightingLoader.Load(
    xshd_reader, ICSharpCode.AvalonEdit.Highlighting.HighlightingManager.Instance
);

xshd_reader.Close();
xshd_stream.Close();

But what if, after I have already set a syntax highlighting definition, I don't want any syntax highlighting, and I just want it to display it as plain text? How can I disable syntax highlighting in the AvalonEdit control?


Solution

  • Have you tried MyEditor.SyntaxHighlighting = null?
    This works for me:

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
    
        <avalonEdit:TextEditor SyntaxHighlighting="C#" x:Name="TextEditor">
            public class Foo
            {
            }
        </avalonEdit:TextEditor>
    
        <Button Grid.Row="1" Content="Reset syntax highlighting" Click="Button_Click" />
    </Grid>
    

    Code-behind:

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            TextEditor.SyntaxHighlighting = null; // highlighting disappears
        }