[英]Dynamic custom content in RichTextBox
I wish to display a text + hyperlinks in a RichTextBox from the code-behind or the binded via the Xaml if there is the possibility. 我希望从代码背后或通过Xaml绑定的RichTextBox中显示文本+超链接。
For the moment, I have a string variable with a Url (that I'd like very much to make clickable) binded to a TextBlock. 目前,我有一个带有Url的字符串变量(我非常想使其可单击)绑定到TextBlock。 I'd like to basically replace: 我想基本上取代:
<TextBlock Text="{Binding myTextWithUrl}" />
by (in a richTB: ) 通过(在richTB中:)
<Run Text="partOfTextNonUrl" /><Hyperlink NavigateUri="theUrl" TargetName="whatever" />
Here is how it is presented: 呈现方式如下:
I have an ItemsControl templated with a custom object 我有一个用自定义对象模板化的ItemsControl
<ItemsControl ItemsSource="{Binding FeedResults}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" >
<my:SearchResultItem />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
And this custom control presents the binded data in 3 TextBlocks as presented above: title, date, and the text containing text + urls. 这个自定义控件将绑定的数据显示在3个TextBlock中,如上所示:标题,日期和包含text + url的文本。
I have already a method that extracts the urls from the string, I just don't know how to use it. 我已经有一个从字符串中提取URL的方法,我只是不知道如何使用它。 I can generate dynamically Run() and Hyperlink(), and add them to the paragraph, but how to bind ? 我可以动态生成Run()和Hyperlink(),并将它们添加到段落中,但是如何绑定?
Or any other solution ? 或任何其他解决方案? You'd make my day!! 你会让我开心的!
Thanks, Sylvain 谢谢,西尔万
I would do something like this. 我会做这样的事情。 Create a ValueConverter which will take your text (with the URL in it). 创建一个ValueConverter,它将获取您的文本(其中包含URL)。 Then in your TextBlock, create the Run and Hyperlink - bind both to the text, both using the ValueConverter, but with a different parameter to the ValueConverter. 然后在您的TextBlock中,创建Run和Hyperlink-都使用ValueConverter将文本和文本都绑定到文本,但要使用与ValueConverter不同的参数。
The 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();
}
}
Then your XAML looks like this: 然后,您的XAML如下所示:
<Run Text="{Binding myTextWithUrl, Converter={StaticResource valueConverter}}"></Run><Hyperlink NavigateUri="{Binding myTextWithUrl, Converter={StaticResource valueConverter}, ConverterParameter=URL}"></Hyperlink>
OK. 好。 So apparently inline Hyperlinks aren't even allowed in Silverlight. 因此,显然在Silverlight中甚至不允许内联超链接。 But you can make your own! 但是你可以自己做!
http://csharperimage.jeremylikness.com/2009/11/inline-hyperlinks-in-silverlight-3.html http://csharperimage.jeremylikness.com/2009/11/inline-hyperlinks-in-silverlight-3.html
Not easy - at least not as easy at it should be. 不容易-至少不应该如此简单。 But it should get the job done. 但这应该可以完成工作。
Once you have the ability to add these runs with hyperlinks, the way I'd approach it is this. 一旦您能够使用超链接添加这些运行,我将采用的方法就是这样。 Create a user control with a single TextBlock ( txtContent
). 用单个TextBlock( txtContent
)创建一个用户控件。 Set the DataContext="{Binding myTextWithUrl}"
. 设置DataContext="{Binding myTextWithUrl}"
。 Then in the code behind: 然后在后面的代码中:
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!"}
};
}
Hope this is helpful. 希望这会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.