Silverlight InlineCollection.Add(InlineUIContainer) 丢失?
我很难将特定类型 InlineUIContainer 的内联添加到 TextBlock 的 InlineCollection (Content 属性)中。看来 InlineCollection 的 .Add() 方法不接受这种类型,但是您可以通过 XAML 清楚地设置它,而无需显式地将内容标记为 InlineContainer,如许多示例所示:
http://msdn.microsoft.com/en-us/library/system.windows.documents.inlineuicontainer .aspx
是否可以通过编程方式添加其中之一,如下所示?
Target.Inlines.Add(new Run() { Text = "Test" });
Target.Inlines.Add(new InlineUIContainer() {
Child = new Image() { Source = new BitmapImage(new Uri("http://example.com/someimage.jpg")) } });
Target.Inlines.Add(new Run() { Text = "TestEnd" });
我有一种感觉,Silverlight 在 XAML 中指定时使用值转换器来创建运行,如示例中不使用 InlineContainer,但我不确定在哪里查找。
我收到的具体错误如下:
Cannot add value of type 'System.Windows.Documents.InlineUIContainer' to a 'InlineCollection' in a 'System.Windows.Controls.TextBlock'.
I'm having difficulty adding the inline of specific type InlineUIContainer into the InlineCollection (Content property) of a TextBlock. It appears the .Add() method of InlineCollection doesn't accept this type, however you can clearly set it through XAML without explicitly marking the content as a InlineContainer, as demonstrated in many examples:
http://msdn.microsoft.com/en-us/library/system.windows.documents.inlineuicontainer.aspx
Is it possible to programatically add one of these as in the following?
Target.Inlines.Add(new Run() { Text = "Test" });
Target.Inlines.Add(new InlineUIContainer() {
Child = new Image() { Source = new BitmapImage(new Uri("http://example.com/someimage.jpg")) } });
Target.Inlines.Add(new Run() { Text = "TestEnd" });
I have a feeling what's going on is that Silverlight is using a value converter to create the runs when specified in XAML as in the example which doesn't use InlineContainer, but I'm not sure where to look to find out.
The specific error I'm getting is as follows:
Cannot add value of type 'System.Windows.Documents.InlineUIContainer' to a 'InlineCollection' in a 'System.Windows.Controls.TextBlock'.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如 Jedidja 所指出的,我们需要使用 RichTextBox 在 Silverlight 中执行此操作。
As pointed out by Jedidja, we need to use RichTextBox to do this in Silverlight.
您不能直接 Add() Runs,但可以添加包含 Runs 的 Spans。
有趣的是,您也可以这样做:
并不是说绕过框架积极试图阻止您做的事情是一个好主意。
PS 如果您无法弄清楚 XAML 正在做什么,请检查可视化树。
You can't Add() Runs directly, but you can add Spans containing Runs.
Interestingly, you can also do this:
Not that it's a good idea to hack around what the framework is actively trying to prevent you from doing.
P.S. If you can't figure out what XAML is doing, inspect the visual tree.