WPF Binding - StringFormat - Not Formatting

后端 未结 7 1557
迷失自我
迷失自我 2020-12-16 12:13

I have a ToolTip with a value set as:

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

The tool

相关标签:
7条回答
  • 2020-12-16 12:39

    Have you tried:

    <ToolTip>
        <TextBlock Text="{Binding Path=DataItem.EquitySold, StringFormat=Reserved (Equity Share: \{0\}%)}" />
    </ToolTip>
    
    0 讨论(0)
  • 2020-12-16 12:42

    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

    0 讨论(0)
  • 2020-12-16 12:44

    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}%)'}"
    
    0 讨论(0)
  • 2020-12-16 12:51

    Try

    StringFormat=Reserved (Equity Share: {0:P0})
    
    0 讨论(0)
  • 2020-12-16 12:52

    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>
    
    0 讨论(0)
  • 2020-12-16 12:56

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

    0 讨论(0)
提交回复
热议问题