2011-11-03 67 views
1

我希望在代碼隱藏或通過Xaml綁定的情況下在RichTextBox中顯示文本+超鏈接(如果有可能的話)。RichTextBox中的動態自定義內容

目前,我有一個綁定到TextBlock的Url的字符串變量(我非常希望可點擊)。我想基本上更換:

<TextBlock Text="{Binding myTextWithUrl}" /> 

通過(在richTB:)

<Run Text="partOfTextNonUrl" /><Hyperlink NavigateUri="theUrl" TargetName="whatever" /> 

這裏是它是如何提出:

我有一個ItemsControl模板與自定義對象

<ItemsControl ItemsSource="{Binding FeedResults}"> 
<ItemsControl.ItemTemplate> 
    <DataTemplate> 
    <StackPanel Orientation="Vertical" > 
    <my:SearchResultItem /> 
    </StackPanel> 
    </DataTemplate> 
</ItemsControl.ItemTemplate> 
</ItemsControl> 

而且這個自定義控件在3個TextBlocks中呈現綁定的數據,如上所示:title,date ,以及包含文字+網址的文字。

我已經有一個方法從字符串中提取url,我只是不知道如何使用它。我可以動態生成Run()和Hyperlink(),並將它們添加到段落中,但是如何綁定?

還是其他解決方案?你會讓我的一天!

謝謝,Sylvain

回答

0

確定。所以顯然,內聯超鏈接在Silverlight中是不允許的。但你可以製造你自己的!

http://csharperimage.jeremylikness.com/2009/11/inline-hyperlinks-in-silverlight-3.html

不容易 - 至少不是那麼容易,它應該是。但它應該完成工作。

一旦你有能力使用超鏈接添加這些運行,我會採取這種方式。使用單個TextBlock創建用戶控件(txtContent)。設置DataContext="{Binding myTextWithUrl}"。然後在代碼背後:

public TextWithUrlUserControl() 
{ 
    InitializeComponent(); 

    this.Loaded += (s, e) => 
         { 
          foreach(var inline in ParseText(DataContext as string)) 
           txtContent.Inlines.Add(inline); 
         }; 
} 

IEnumerable<Inline> ParseText(string text) 
{ 
    // return list of Runs and Runs with hyperlinks using your URL parsing 
    // for demo purposes, just hardcoding it here: 
    return new List<Inline> 
       { 
        new Run{Text="This text has a "}, 
        new Run{Text="URL", RunExtender.NavigateUrl="http://www.google.com/"}, 
        new Run{Text="in it!"} 
       };  
} 

希望這是有幫助的。

+0

你好亞當,它確實工作,但是我帶着另一個解決方案感謝Adams Sills post(http://blogs.catapultsystems.com/asills /archive/2010/11/17/fun-with-attached-properties-%E2%80%93-bindable-richtextbox-xaml.aspx),我可以綁定Xaml屬性。這是我所做的:http://mysharpsnippets.wordpress。com/2011/11/06/dynamic-content-in-richtextbox/ 再次感謝您的幫助;) – Slyvain

0

我會做這樣的事情。創建一個ValueConverter,它將帶您的文本(包含其中的URL)。然後在你的TextBlock中,創建運行和超鏈接 - 將兩者都綁定到文本,都使用ValueConverter,但使用與ValueConverter不同的參數。

的ValueConverter:

public class MyCustomValueConverter: IValueConverter 
{  
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     if(parameter.ToString()== "URL") 
     { 
      // return the URL part of the string 
     } 
     else 
     { 
      // return the non-URL portion of the string 
     } 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

然後你的XAML看起來像這樣:

<Run Text="{Binding myTextWithUrl, Converter={StaticResource valueConverter}}"></Run><Hyperlink NavigateUri="{Binding myTextWithUrl, Converter={StaticResource valueConverter}, ConverterParameter=URL}"></Hyperlink> 
+0

感謝亞當,如果我知道我有文本+超鏈接,這將工作,訣竅是,我不知道如果我有一個url或幾個文本。我可以沒有或3例如。所以我需要,不知何故,動態生成運行和超鏈接。 – Slyvain

+0

啊。得到它了。我很快就會添加另一個答案,代表我可能會採取的方式... –

相關問題