Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does StringFormat have no effect on the binding of my MenuItem.Header?

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>
like image 205
Eric Ouellet Avatar asked Mar 26 '15 01:03

Eric Ouellet


2 Answers

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}">
like image 142
Eren Ersönmez Avatar answered Nov 09 '22 17:11

Eren Ersönmez


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>
like image 14
a little sheep Avatar answered Nov 09 '22 19:11

a little sheep