All of my 6 samples have "StringFormat" in their binding but none is applied and I'm only getting the value without any formatting.
Any idea what I do wrong?
<MenuItem Header="{Binding SeriesNearMouse.Count, StringFormat='Series {0}'}">
</MenuItem>
<MenuItem DataContext="SeriesNearMouse">
<MenuItem.Header>
<Binding Path="Count"
StringFormat="Series{0:D}test">
</Binding>
</MenuItem.Header>
</MenuItem>
<MenuItem>
<MenuItem.Header>
<Binding Path="SeriesNearMouse.Count"
StringFormat="Series{0}">
</Binding>
</MenuItem.Header>
</MenuItem>
<MenuItem Header="{Binding SeriesNearMouse.Count, StringFormat='{}Series {0}'}">
</MenuItem>
<MenuItem DataContext="SeriesNearMouse">
<MenuItem.Header>
<Binding Path="Count"
StringFormat="{}Series{0:D}test">
</Binding>
</MenuItem.Header>
</MenuItem>
<MenuItem>
<MenuItem.Header>
<Binding Path="SeriesNearMouse.Count"
StringFormat="{}Series{0}">
</Binding>
</MenuItem.Header>
</MenuItem>
LATE ANSWER
Header
is not a string property. It is actually an object property that gets presented in a ContentPresenter
. In order to pass the string format to the ContentPresenter
, MenuItem
provides a HeaderStringFormat
property:
<MenuItem Header="{Binding SeriesNearMouse.Count}" HeaderStringFormat="Series {0}">
It is probably not working how you expect because MenuItem.Header is an object, not a string.
Something like this should work, since Run.Text is a string:
<MenuItem>
<MenuItem.Header>
<Run Text="{Binding SeriesNearMouse.Count, StringFormat='Series {0}', Mode=OneWay}" />
</MenuItem.Header>
</MenuItem>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With