Creating an Instance WPF InlineCollection
I'm populating a class programtically at runtime, and started by building a collection of InlineColl开发者_如何学Goection classes. However, InlineCollection class cannot be instantiated.
My question is, how do I add a collection of inlines to Span.Inlines whose type is InlineCollection, if I can't create an Instance of it?
Basically I need a collection of a collection of Inline classes, so I can randomally set Span.Inlines to a new collection of Inline classes.
yes, as you cannot instantiate InlineCollection
class, but what you can do is to use for example List<Inline>
and populate this.
later it is easy to apply them to, for example, a TextBlock
:
// create some inlines
List<Inline> inlines = new List<Inline>();
inlines.Add(new Run() { Text = "text" });
Span span = new Span();
span.Inlines.AddSafe(new Run() { Text = "text inside span" });
inlines.Add(span);
// now apply to a TextBlock
TextBlock tb = new TextBlock() { TextWrapping = TextWrapping.Wrap };
tb.Inlines.Clear();
foreach (Inline i in inlines)
tb.Inlines.Add(i);
Have a look here: http://msdn.microsoft.com/en-us/magazine/cc163371.aspx
It looks like what you want to do is:
span.Inlines.Add(new Run("Some normal text"));
var b = new Bold();
b.Inlines.Add(new Run(" Some bold text"));
span.Inlines.Add(b);
精彩评论