简体   繁体   中英

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?

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
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM