开发者

WPF Binding - StringFormat - Not Formatting

I have a ToolTip with a value set as:

Value="{Binding Path=DataItem.EquitySo开发者_开发问答ld, StringFormat=Reserved (Equity Share: \{0\}%)}"

The toolip is displaying as:

72

But I expect it to be:

Reserved (Equity Share: 72%)

What is wrong with my binding?


A toolTip is a content control, which means it doesn't really have a display model. This is demonstrated in the earlier answer by @deccyclone that sets the content to a TextBlock. Since the TextBox is designed to display text, the StringFormat binding property works as advertised. Button is another example of this. (Both derive from ContentControl)

If you set the Content of a ToolTip to a string, the string is displayed because the ToolTip has a built in converter if the dataType is string. If you want to take advantage of that built in string converter, you need to set the format using the ContentStringFormat property.

<ToolTip
     Content="{Binding Path=Value}"
     ContentStringFormat="{}{0:F2} M"
/>

BTW, the tip off for when to use StringFormat or ContentStringFormat is by which property the control supplies for setting the displayed text. Text property -> use StringFormat Content property -> use ContentStringFormat


Have you tried:

<ToolTip>
    <TextBlock Text="{Binding Path=DataItem.EquitySold, StringFormat=Reserved (Equity Share: \{0\}%)}" />
</ToolTip>


You don't need to escape the brackets. Try this (i like to put the format in single quotes):

Value="{Binding Path=DataItem.EquitySold, StringFormat='Reserved (Equity Share: {0}%)'}"


I assume it is what your datatype supports - as far as I know it is passed on as arguments to IFormattable.


<Button.ToolTip>
 <TextBlock Text="{Binding Path=ToggleText, StringFormat={}{0} Text}"/>
</Button.ToolTip>

Button inside DataGridTemplateColumn


For anyone else that winds up here in a slightly different situation this was desired for setting a tooltip StringFormat via Style:

<DataGridTextColumn Header="Amount" CanUserSort="True"
                                    Binding="{Binding Amount,Mode=OneWay}">
    <DataGridTextColumn.CellStyle>
        <Style TargetType="DataGridCell">
            <Setter Property="ToolTip">
                <Setter.Value>
                    <TextBlock Text="{Binding JournalEntryId, StringFormat='Reserved (Equity Share: \{0\}%)'}" />
                </Setter.Value>
            </Setter>
        </Style>
    </DataGridTextColumn.CellStyle>
</DataGridTextColumn>


Try

StringFormat=Reserved (Equity Share: {0:P0})
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜