开发者

Sl 4, MVVM: Using Inlines in a TextBlock, how to bind to the ViewModel?

We format text for a TextBlock by addin开发者_StackOverflowg a series of System.Windows.Documents.Run objects to TextBlock.Inlines.

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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜