Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Work with Fast Colored TextBox for Syntax Highlighting in WPF

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#.

like image 817
Dany Maor Avatar asked Feb 18 '16 10:02

Dany Maor


3 Answers

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 {  }";
like image 127
zion Avatar answered Nov 17 '22 01:11

zion


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}"/>
like image 31
Murat Aykanat Avatar answered Nov 17 '22 00:11

Murat Aykanat


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 };
        }
    }
}
like image 1
CodingYourLife Avatar answered Nov 17 '22 02:11

CodingYourLife