Sl 4, MVVM: Using Inlines in a TextBlock, how to bind to the ViewModel?
How can we bind the formatted text in the ViewModel to display in the TextBlock?
Thanks for any advice...
I attempted creating an InlineCollection property in aViewModel, then set the binding in the xaml, but the InlineCollection in TextBlock is not bindable, since it is not a DependencyProperty. The answer to this question shows creating your own control and making it a DependencyProperty which is an ok workaround.
VM:
private InlineCollection inlineCollection;
public InlineCollection TextBlockInlineCollection {
get
{
return inlineCollection;
}
set
{
inlineCollection = value;
NotifyPropertyChanged("TextBlockInlineCollection");
}
}
xaml: // doesn't work
You could also create the TextBlock dynamically in code using the XamlReader: example:
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);
精彩评论