Work with Fast Colored TextBox for Syntax Highlighting in WPF

跟風遠走 提交于 2019-12-07 04:51:07

问题


Is it possible to work with Fast Colored TextBox for Syntax Highlighting 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#.


回答1:


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

In the code you create an instance of the FastColoredTextBox and add it to the windows forms host object like the example below:

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

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



回答2:


I know it's not really answering your question but I found myself in the same situation asking that question and I solved it by using AvalonEdit instead. You can just WPF Bind to the Document Property and I even used it inside a Tooltip.

Here a minimal example...

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>

The class Bound to (ItemSource is SnippetList, that's a List of Snippet:

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

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



回答3:


You need to create a custom WindowsFormsHost control and add your bindings as dependency properties to this custom control to pass it onto the FastColoredTextBox. A more general example is explained here.

But for this specific problem of binding the Text property:

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);
            }
        }
    }
}

And the XAML:

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



回答4:


Child = _innerTextbox;

        _innerTextbox.TextChanged += _innerTextbox_TextChanged;

It works for me fine



来源:https://stackoverflow.com/questions/35478456/work-with-fast-colored-textbox-for-syntax-highlighting-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!