使用快速彩色TextBox在WPF中进行语法突出显示

时间:2016-02-18 10:09:54

标签: syntax highlighting

是否可以使用快速彩色文本框在WPF中进行语法突出显示。

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

我找不到适用于WPF c#的示例。

4 个答案:

答案 0 :(得分:4)

是的,但您必须使用名为 WindowsFormsHost 的WPF组件。

在代码中,您创建FastColoredTextBox的实例并将其添加到Windows窗体主机对象,如下例所示:

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

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

答案 1 :(得分:2)

我知道它并没有真正回答你的问题,但我发现自己处于相同的情况下提出这个问题而我通过使用AvalonEdit解决了这个问题。你可以将WPF绑定到Document Property,我甚至在Tooltip中使用它。

这是一个最小的例子......

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>

类绑定到(ItemSource是SnippetList,它是一个Snippet列表:

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

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

答案 2 :(得分:0)

您需要创建自定义WindowsFormsHost控件,并将绑定作为依赖项属性添加到此自定义控件,以将其传递到FastColoredTextBox。 更一般的例子被解释为here

但是对于绑定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}"/>

答案 3 :(得分:0)

Child = _innerTextbox;

var Human = {
  eyes: 2,
  feets: 2

}

var Mark = Object.create(Human);

对我来说很好