StringFormat binding does not work correctly

I have a DataGrid and Expander like this:

<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, line formatting will not work. It always only displays the value dataGrid1.SelectedItem.Name I also tried:

 StringFormat=Details of \{0\} 

which does not work.

I even tried just setting the HeaderStringFormat Expander property to "Details from {0}", but that also does not format it.

I managed to get this workaround to work:

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

Does anyone know why StringFormat is not working for the Header property?

+7
wpf binding
source share
3 answers

According to http://codingcontext.wordpress.com/2008/11/17/headerformatatring-and-contentformatstring/ , it looks like the HeaderStringFormat property HeaderStringFormat not intended to be used with string format binding, but rather, specify the format used when binding to the object. which implements IFormattable .

Given that I could not get string formatting to work directly in the binding expression, so this might just be a mistake. You should try to notify Microsoft and possibly fix it.

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

+4
source share

I also encountered the same problem and after reading some articles, tried all kinds of ContentStringFormat and HeaderStringFormat. I just decided to do the following:

 <Expander Grid.Row="1" Padding="4"> <Expander.Header> <TextBlock Text="{Binding ElementName=cbCategory, Path=SelectedItem.CategoryName, StringFormat='FORMATTED &quot;{0}&quot;'}"/> </Expander.Header> 
+3
source share

Perhaps this is due to the fact that the header is a property of the object type, not a string. Converting a String value to a user interface control may interfere with formatting. Instead of TextBox, does Label work? This should give you the same effect as what you originally tried to do.

Update : Also try examining the HeaderStringFormat property.

0
source share

All Articles