Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding to Static class property and StringFormat

I am able to bind a static class property to a MenuItem header, but I cannot determine how to include a StringFormat so that I can display hard-coded text in addition to the property.

Is this possible?

Currently: (Displays "SQLSERVER1")

Header="{x:Static settings:Settings.CurrentServer}"

Desired: (Display "Connection: SQLSERVER1")

Header="{Binding Source={x:Static Settings:Settings.CurrentServer},StringFormat='Connection: {0}'}"

When I try the 'Desired' line in the XAML the StringFormat is ignored entirely. What am I doing wrong?

like image 269
Joe Bauer Avatar asked Oct 08 '15 18:10

Joe Bauer


1 Answers

MenuItem provides a HeaderStringFormat property that you should use:

<MenuItem Header="{Binding Source={x:Static Settings:Settings.CurrentServer}}"
          HeaderStringFormat="Connection: {0}" />

Actually, that property is part of HeaderedItemsControl, which MenuItem happens to extend.

The StringFormat property is just ignored.

like image 132
Grant Winney Avatar answered Oct 13 '22 17:10

Grant Winney