Question

I need to create a TreeView type controller that will display rich text.

Example:

» Signed by Person Name

» Certified by Person Name

Content of the TreeViewItem is easy as I can simply put TextBlock inside, but header don't allow for multiple font style declarations.

I'm really a beginner in XAML and I work in ExpressionBlend (although I don't shy away from coding). I would really appreciate if someone push me into right direction.

Was it helpful?

Solution

You can put a "Run" in a TextBlock to change the font.

e.g.

<TextBlock>Hello<Run FontStyle="Bold">World</Run></TextBlock>

You can set the content of the header to anything you want

<TreeViewItem>
    <TreeViewItem.Header>
        <TextBlock>Hello<Run FontStyle="Bold">World</Run></TextBlock>
    </TreeViewItem.Header>
</TreeViewItem>

When you do this:

<TreeViewItem>Hello</TreeViewItem>

You are not telling the XAML parser what property you want to put 'Hello' into. The XAML parser will look at the type (TreeViewItem) and see which is set as the 'ContentProperty', in this case it's the property called 'Header'. Because you have not specified what 'Hello' is, it assumes a string. Since a string can't actually be displayed as content in WPF, it has to create a control to display the string. The ValueConverter for TreeViewItem is set up to provide a TextBlock control and set the Text of this control to the string you entered. So what's happening behind the scenes is a lot more than you can see :)

All of this stuff you can do with your own controls

You should probably read up on WPF templating, content controls, content presenter, styles etc. There are a lot of powerful things you can do with WPF - for instance, make every single button in your application have the same layout and style and add an image to the front of the button without any code (just with a few lines of XAML)

Have a look here for a beginners guide to templating

http://msdn.microsoft.com/en-us/magazine/cc163497.aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top