我们通过向 TextBlock.Inlines 添加一系列 System.Windows.Documents.Run 对象来格式化 TextBlock 的文本。
我们如何绑定ViewModel中的格式化文本以显示在TextBlock中?
感谢您的任何建议...
我尝试在 aViewModel 中创建 InlineCollection 属性,然后在 xaml 中设置绑定,但 TextBlock 中的 InlineCollection 不可绑定,因为它不是 DependencyProperty。答案对于这个问题 http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/82b30e02-aac4-4564-9e3b-05d5622b9005/显示创建您自己的控件并将其设为 DependencyProperty,这是一个不错的解决方法。
VM:
private InlineCollection inlineCollection;
public InlineCollection TextBlockInlineCollection {
get
{
return inlineCollection;
}
set
{
inlineCollection = value;
NotifyPropertyChanged("TextBlockInlineCollection");
}
}
xml:
// 不起作用
您还可以使用 XamlReader 在代码中动态创建 TextBlock:
例子:
string textBlock = @"<TextBlock xmlns='http://schemas.microsoft.com/client/2007' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' x:Name='infoBarTextBlockWithFormatting' AutomationProperties.AutomationId='infoBarTextBlockWithFormatting' Margin='9,0,0,0' TextWrapping='Wrap'>";
textBlock = String.Concat(textBlock, e.NewValue.ToString(), "</TextBlock>");
infoBar.infoBarRunHolder.Child = (TextBlock)XamlReader.Load(textBlock);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)