wpf - 使用字符串文字在标签上绑定字符串格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3969224/
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-08 22:14:47  来源:igfitidea点击:

wpf - binding stringformat on label using string literal

wpfxamlstring-formatting

提问by TWood

I've binded the tooltip of a slider control to it's Value property and i'm trying to use StringFormat to make it display "Current Value {0} of 10" where the {0} is the Value property. Below is one of the various things I tried when trying to figure this out.

我已将滑块控件的工具提示绑定到它的 Value 属性,我正在尝试使用 StringFormat 使其显示“Current Value {0} of 10”,其中 {0} 是 Value 属性。以下是我在尝试解决此问题时尝试过的各种方法之一。

<Slider.ToolTip>
  <Label>
    <Label.Content>
      <Binding StringFormat="Current Value {0} of 10"
               ElementName="DebugLevelSlider"
               Path="Value" />
    </Label.Content>
  </Label>
</Slider.ToolTip>

I am having issues finding examples online of how to use stringformat with string literals such as mine above. I see a lot of stringformat date/time/currency format conversion. I think I have a way to do this with a multibinding but it just seems like an extra amount of work than necessary. I hope that for string literal formatting I still do not have to write a custom converter.

我在网上查找有关如何将 stringformat 与字符串文字结合使用的示例时遇到问题,例如我上面的。我看到很多 stringformat 日期/时间/货币格式转换。我想我有一种方法可以通过多重绑定来做到这一点,但这似乎是不必要的额外工作。我希望对于字符串文字格式,我仍然不必编写自定义转换器。

In my app I find myself using a lot of extra labels next to items so getting an understanding in the stringformat will hopefully let me eliminate some of those unnecessary labels.

在我的应用程序中,我发现自己在项目旁边使用了很多额外的标签,因此了解 stringformat 有望让我消除一些不必要的标签。

回答by Guillermo Ruffino

Label.Contentis object so you can't use Binding.StringFormatthere as the binding's target type must be stringin order for it to work.

Label.Content是对象,所以你不能Binding.StringFormat在那里使用,因为绑定的目标类型必须是string它才能工作。

Two work arounds are: use TextBlockinstead of Labeland bind the Textproperty.

两种解决方法是:使用TextBlock代替Label和绑定Text属性。

Use Label.ContentStringFormati.e.

使用Label.ContentStringFormat

<Label ContentStringFormat="Current Value {0} of 10" Content={Binding ...} />

You only need to escape the stringwith {}if your first character is a {

如果您的第一个字符是 a,您只需要转义stringwith{}{

回答by a_hardin

For the ToolTip, you can check out WPF binding with StringFormat doesn't work on ToolTips.

对于 ToolTip,您可以查看WPF 绑定与 StringFormat 不适用于 ToolTips

As far as the StringFormat you specified above, you have to escape your string.

就您在上面指定的 StringFormat 而言,您必须转义您的字符串。

StringFormat="{}Current Value {0} of 10"

Here are a bunch of StringFormat examples. http://blogs.msdn.com/b/llobo/archive/2008/05/19/wpf-3-5-sp1-feature-stringformat.aspx

这里有一堆 StringFormat 示例。 http://blogs.msdn.com/b/llobo/archive/2008/05/19/wpf-3-5-sp1-feature-stringformat.aspx