2016-02-18 70 views

回答

3

是的,但您將不得不使用WPF組件WindowsFormsHost

在創建FastColoredTextBox的一個實例,並將其添加到Windows的代碼構成宿主對象如下面的例子:

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

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

我知道這是不是真的回答你的問題,但我發現自己在同問這個問題,我用AvalonEdit來解決問題。你可以只是WPF綁定到文檔屬性,我甚至在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,這是片段的列表中的類:

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

    public TextDocument Document { 
     get { 
      return new TextDocument(){ Text = this.Data }; 
     } 
    } 
} 
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}"/>