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#.
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 { }";
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}"/>
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 };
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With