Databinding TextBlocks with XAML
I’ve become frustrated lately with trying to databind a list of strings to a textblock. Ie, if I have:
string[] mytext = new string[] { "a", "b", "c" };
And I want the effective end result to be:
<TextBlock>a, b, c</TextBlock>
Or maybe I want something more complex like hyperlinks:
<TextBlock> <Hyperlink>a</Hyperlink>, <Hyperlink>b</Hyperlink>, <Hyperlink>c</Hyperlink> </TextBlock>
Basically what I’m looking for is a ItemsControl that works on TextBlocks (or Spans, etc.), with a Separator template to insert those “, ” in between the items.
Your first instinct might be to use a StackPanel instead, but StackPanel wasn’t made for displaying text. It might fool you initially, but you quickly notice it doesn’t behave anything like text: there’s no proper kerning, RTL, wrapping, or anything else the text classes were made to support.
I’m pretty surprised that WPF doesn’t have anything like this, as it seems like displaying lists as text would be a common enough thing for any app. Unfortunately I’m still not well versed enough in WPF to create such a custom control for myself, and haven’t had a whole lot of time to investigate it.
Related Posts
- Alex Filo has the right idea, UniformWrapPanel on March 06, 2009 in .NET Framework, C#, Coding, WPF, XAML
- Converting SVG to XAML on June 01, 2008 in .NET Framework, Coding, WPF, XAML
- Visual Studio 2010 CTP now available on October 28, 2008 in C#, Coding
- Using HSL colors in WPF on March 01, 2009 in .NET Framework, C#, Coding, WPF, XAML
- C#: lamer by the moment on November 04, 2008 in .NET Framework, C#, Coding