tags:

views:

539

answers:

2

I have a DataGrid and an Expander like so:

<StackPanel>
<my:DataGrid Name="dataGrid1" AutoGenerateColumns="False"  ItemsSource="{Binding}">...</my:DataGrid>
<Expander Header="{Binding ElementName=dataGrid1, Path=SelectedItem.Name, StringFormat=Details of {0}}">...</Expander>
</StackPanel>

The binding is fine, but for some reason the string formatting will not work. It always displays only the value dataGrid1.SelectedItem.Name I have also tried:

StringFormat=Details of \{0\}

which doesn't work.

I even tried just setting the HeaderStringFormat property of the Expander to "Details of {0}" but that doesn't format it either.

I was able to get this workaround to work though:

<Expander>
<Expander.Header>
<TextBox Text="{Binding ElementName=dataGrid1, Path=SelectedItem.Name, StringFormat=Details of {0}}"></TextBox>
</Expander.Header>
</Expander>

Anyone know why StringFormat isn't working for the Header property though?

A: 

It may have something to do with Header being an Object type property rather than String. The conversion of the String value to a UI control may be interfering with the formatting. Rather than TextBox, does Label work as well? That should give you the same effect as what you were initially trying to do.

Update: Also try looking into the HeaderStringFormat property.

Volte
You do have a point about Header being an object and TextBox.Text being a string. I just tried with a label, however, and it worked when I set the ContentFormatString to what I wanted. The Label Content property is also an object so I still don't understand...something strange with the Header.
AdamD
+1  A: 

According to http://codingcontext.wordpress.com/2008/11/17/headerformatstring-and-contentformatstring/, it looks like the HeaderStringFormat property isn't meant to be used with string format binding, but rather to specify the format to use when binding to an object that implements IFormattable.

Given that, I couldn't get string formatting to work directly in the binding expression, either, so that might just be a bug. You should try notifying Microsoft and maybe they'll fix it.

With your workaround, I would suggest using a TextBlock rather than a TextBox, since you probably don't want the user to be able to edit the text in the Expander header.

Andy
Alright, that's informative. Thanks. Also, I am using a TextBlock...I guess I just wrote it wrong when I made the post.
AdamD