1

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?

Brandon Miller
  • 2,247
  • 8
  • 36
  • 54

1 Answers1

1

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
    }
Dennis
  • 37,026
  • 10
  • 82
  • 150
  • That gives me a `Object reference not set to an instance of an object` error. – Brandon Miller Jan 13 '13 at 06:06
  • @BrandonMiller: I've checked it myself and updated the answer - setting `null` value works fine (I've used WPF application). Could you post stack trace of NRE, to be sure, that it is a AvalonEdit's problem? – Dennis Jan 13 '13 at 07:44