views:

24

answers:

1

I am trying to get the header text of a TreeviewItem to be set in the binding to an XML source. Everything is working fine except the only thing that appears in the header is the text I'm binding to and nothing else in the string format. Example:

<HierarchicalDataTemplate x:Key="LogDataTemp" ItemsSource="{Binding Path=log}">
    <TreeViewItem>
        <TreeViewItem.Header>
            <Binding Path="Attribute[level].Value" StringFormat="TEST \{0\}" />
        </TreeViewItem.Header>
    </TreeViewItem>
</HierarchicalDataTemplate>

In this case the level value appears but nothing else. I have tried dozens of different ways of approaching this and it seems that nothing works.

+1  A: 

Don't escape the braces in the StringFormat. You want to apply the formatting to the 0th-element in your binding.

For instance, with a simple property called "Level":

        <TextBlock x:Name="txtUnformatted" Grid.Row="0" Foreground="White" >
            <TextBlock.Text>
                <Binding Path="Level" />
            </TextBlock.Text>
        </TextBlock>

        <TextBlock x:Name="txtFormatted" Grid.Row="1" Foreground="White">
            <TextBlock.Text>
                <Binding Path="Level" StringFormat="Test {0:000000}" />
            </TextBlock.Text>
        </TextBlock>

And the result is something like:
alt text

Update

Also, the default implementation of the Header, when you don't add any controls, is a simple ContentPresenter, which doesn't apply formatting. To work around this, simply put a TextBlock into the header, and bind the text you want formatted to that.

<HierarchicalDataTemplate x:Key="LogDataTemp" ItemsSource="{Binding Path=log}">
    <TreeViewItem>
        <TreeViewItem.Header>
            <TextBlock>
                <TextBlock.Text>
                     <Binding Path="Attribute[level].Value" 
                              StringFormat="TEST {0}" />
                </TextBlock.Text>
            </TextBlock>
        </TreeViewItem.Header>
    </TreeViewItem>
</HierarchicalDataTemplate>

It is perfectly acceptable (and commonly done) to put controls into a header control (for instance, a grid containing an image and a label). The beauty of WPF.

Wonko the Sane
My initial thought too, but it doesn't explain why the bound value appears rather than the string literal "TEST {0}"...
Kent Boogaart
I've tried without the escaping as well. Just the bound value appears. I just tried changing it to the format you suggested "{0:000000}" and still just the bound value appears and not "Test ".
Mark
I am wondering if it has something to do with it being a header instead of a text block in the case of what you posted.
Mark
Ok confirmed I added a textblock inside the TreeviewItem and used the same stringformat and test appears fine there. Any ideas?
Mark
It's because the internal representation of the Header, when you don't use a control, is a simple ContentPresenter, which doesn't apply any formatting. If you simply put a TextBlock inside the header, it will use that to display the content, and as you have seen, you can format it. There is certainly nothing wrong with putting a control in the Header, and is common practice (for instance, to put a grid that contains an icon and a textblock). I will update my answer to reflect that.
Wonko the Sane
Wow hadn't thought of that one! Just tried it and it works, thank you for the help!
Mark