Working with fast color TextBox to highlight syntax in WPF

Is it possible to work with Fast Colored TextBox to highlight syntax in WPF.

http://www.codeproject.com/Articles/161871/Fast-Colored-TextBox-for-syntax-highlighting

I could not find an example that works with WPF C #.

+4
source share
3 answers

Yes, but you will have to use a WPF component called WindowsFormsHost .

In the code, you create an instance FastColoredTextBoxand add it to the window host object, as shown below:

FastColoredTextBox textBox = new FastColoredTextBox();
windowsFormsHost.Child = textBox;

textBox.TextChanged += Ts_TextChanged;
textBox.Text = "public class Hello {  }";
+3
source

, , , , , AvalonEdit . WPF Document, .

...

WPF:

<ListBox ItemsSource="{Binding SnippetList}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid HorizontalAlignment="Stretch">
                <Grid.ToolTip>
                    <avalonEdit:TextEditor xmlns:avalonEdit="http://icsharpcode.net/sharpdevelop/avalonedit" Name="FctbPreviewEditor" FontFamily="Consolas" SyntaxHighlighting="C#" FontSize="10pt" Document="{Binding Document}" />
                </Grid.ToolTip>

                <TextBlock Text="{Binding Label}" Grid.Column="1" />
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Bound to (ItemSource - SnippetList, :

public class Snippet
{
    public string Label { get; set; }
    public string Data { get; set; }

    public TextDocument Document {
        get {
            return new TextDocument(){ Text = this.Data };
        }
    }
}
+2

WindowsFormsHost , FastColoredTextBox. .

Text:

using System.Windows;
using System.Windows.Forms.Integration;
using FastColoredTextBoxNS;

namespace ProjectMarkdown.CustomControls
{
    public class CodeTextboxHost : WindowsFormsHost
    {
        private readonly FastColoredTextBox _innerTextbox = new FastColoredTextBox();

        public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(CodeTextboxHost), new PropertyMetadata("", new PropertyChangedCallback(
            (d, e) =>
            {
                var textBoxHost = d as CodeTextboxHost;
                if (textBoxHost != null && textBoxHost._innerTextbox != null)
                {
                    textBoxHost._innerTextbox.Text = textBoxHost.GetValue(e.Property) as string;
                }
            }), null));

        public CodeTextboxHost()
        {
            Child = _innerTextbox;

            _innerTextbox.TextChanged += _innerTextbox_TextChanged;
        }

        private void _innerTextbox_TextChanged(object sender, TextChangedEventArgs e)
        {
            SetValue(TextProperty, _innerTextbox.Text);
        }

        public string Text
        {
            get { return (string) GetValue(TextProperty); }
            set
            {
                SetValue(TextProperty, value);
            }
        }
    }
}

XAML:

<customControls:CodeTextboxHost Text="{Binding MyTextField, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
0

Source: https://habr.com/ru/post/1629342/


All Articles