What is the WPF XAML Data Binding equivalent of String.Format?

前端 未结 5 1618
半阙折子戏
半阙折子戏 2020-12-05 00:13

Or, to be more clear, how can I format a block of text (in my case, to be included within a tooltip) such that some portions of the text come from bound values.

In p

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-05 00:39

    As far as I know, WPF doesn't do what you want. You do have a much more powerful (albeit more involved) solution.

    Take a look at the IValueConverter interface.

    MSDN HowTo link here

    EDIT

    Based on aku's answer, and your assertion that you can't use 3.5 SP1, here's an alternative.

    Take a look at Phil Haack's recent series of posts on string formatting:

    • Fun With Named Formats, String Parsing, and Edge Cases
    • Named Formats Redux

    Create a ValueConverter as that takes the format as a property. You should then be able to bind your data object and have it format based on your defined format (using property name instead of position).

提交回复
热议问题