.net 什么是 String.Format 的 WPF XAML 数据绑定等效项?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/447035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 11:00:03  来源:igfitidea点击:

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

.netwpfbindingformattingtext-formatting

提问by Drew Noakes

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 plain C# I would use:

在普通的 C# 中,我会使用:

_toolTip.Text = string.Format("{1:#0}% up, {2:#0}% down",
    Environment.NewLine, percentageOne, percentage2);

However the WPF XAML markup for a Text property seems able to contain only a single binding. The curly braces gave me high hopes, but this isn't possible:

但是,Text 属性的 WPF XAML 标记似乎只能包含一个绑定。花括号给了我很高的期望,但这是不可能的:

<Element>
  <Element.Tooltip>
    <!-- This won't compile -->
    <TextBlock Text="{Binding Path=PercentageOne}% up, {Binding Path=PercentageTwo}% down"/>
  </Element.Tooltip>
</Element>

I read that the Run.Textproperty is not a dependency property and can therefore not be bound.

我读到该Run.Text属性不是依赖属性,因此无法绑定。

Is there a way I can perform this formatting in XAML?

有没有办法在 XAML 中执行这种格式设置?

回答by aku

You can use MultiBinding + StringFormat (requires WPF 3.5 SP1):

您可以使用 MultiBinding + StringFormat(需要 WPF 3.5 SP1):

<TextBox.Text>
    <MultiBinding StringFormat="{}{1:#0}% up, {2:#0}% down">
      <Binding Path="PercentageOne" />
      <Binding Path="PercentageTwo"/>
    </MultiBinding>
</TextBox.Text>

Regarding Run.Text - you can't bind to it but there are some workarounds:

关于 Run.Text - 您无法绑定到它,但有一些解决方法:

回答by Dave

I would split into multiple textblocks, binding each one with the StringFormat={0:P} in the binding as such:

我将拆分为多个文本块,将每个文本块与绑定中的 StringFormat={0:P} 绑定,如下所示:

<TextBox Text="{Binding Something, StringFormat=\{0:P\}}" />

See this post for examples:Lester's WPF Blog on StringFormat

有关示例,请参阅此帖子:Lester 的 WPF 博客 StringFormat

Checkout VS2010 - The binding from properties includes formatting in the options.

Checkout VS2010 - 来自属性的绑定包括选项中的格式。

回答by Kent Boogaart

If you're using 3.5 SP1, Aku's answer is the way to go. If you're not, you can use the FormatConverterfrom my WPF Converterslibrary.

如果您使用的是 3.5 SP1,那么 Aku 的答案就是您要走的路。如果不是,您可以使用我的WPF 转换器库中的FormatConverter

回答by Michael Meadows

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

据我所知,WPF 不会做你想做的事。您确实有一个更强大(尽管更复杂)的解决方案。

Take a look at the IValueConverterinterface.

看看IValueConverter接口。

MSDN HowTo link here

MSDN HowTo 链接在这里

EDIT

编辑

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

根据 aku 的回答,以及您不能使用 3.5 SP1 的断言,这里有一个替代方案。

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

看看 Phil Haack 最近关于字符串格式的系列文章:

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).

创建一个 ValueConverter,因为它将格式作为属性。然后您应该能够绑定您的数据对象并根据您定义的格式(使用属性名称而不是位置)设置格式。

回答by Gabe

The way I've solved this in the past is actually to break the TextBlock you have in your listing up into several TextBlocks. Try something like this:

我过去解决这个问题的方法实际上是将列表中的 TextBlock 分解为多个 TextBlock。尝试这样的事情:

<Element>
  <Element.Tooltip>
    <StackPanel Orientation="Horizontal">
      <TextBlock Text="{Binding Path=PercentageOne}"/>
      <TextBlock Text="% up, "/>
      <TextBlock Text="{Binding Path=PercentageTwo}"/>
      <TextBlock Text="% down"/>
    </StackPanel>
  </Element.Tooltip>
</Element>

Alternately you can create something like a StringFormatConverter, which could take the format string as a parameter, and use a MultiBinding to pass it the parameters. See this link for MultiBindings:

或者,您可以创建类似 StringFormatConverter 的东西,它可以将格式字符串作为参数,并使用 MultiBinding 将参数传递给它。有关 MultiBindings,请参阅此链接:

MultiBinding Info

多重绑定信息

And this one for info on converters:

这是关于转换器的信息:

Converters Info

转换器信息

You can pretty easily imagine a converter that takes "object[] values" instead of "object value" as it's first parameter, and passes those on to the Format function.

您可以很容易地想象一个转换器将“object[] values”而不是“object value”作为第一个参数,并将它们传递给 Format 函数。