标签内容上的 WPF StringFormat

发布于 2024-10-02 23:39:43 字数 473 浏览 1 评论 0原文

我想将字符串绑定格式设置为 Amount is X,其中 X 是绑定到标签的属性。

我见过很多例子,但以下不起作用:

<Label Content="{Binding Path=MaxLevelofInvestment, 
   StringFormat='Amount is {0}'}" />

我也尝试过这些组合:

StringFormat=Amount is {0}
StringFormat='Amount is {}{0}'
StringFormat='Amount is \{0\}'

我什至尝试将绑定属性的数据类型更改为 intstring 和 <代码>双。似乎什么都不起作用。这是一个非常常见的用例,但似乎不受支持。

I want to format my string binding as Amount is X where X is a property bound to a label.

I've seen many examples but the following doesn't work:

<Label Content="{Binding Path=MaxLevelofInvestment, 
   StringFormat='Amount is {0}'}" />

I've also tried these combinations:

StringFormat=Amount is {0}
StringFormat='Amount is {}{0}'
StringFormat='Amount is \{0\}'

I even tried changing the binding property's datatype to int, stringand double. Nothing seems to work. This is a very common use case but doesn't seem to be supported.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

甚是思念 2024-10-09 23:39:43

这不起作用的原因是 Label.Content 属性的类型为 Object,并且 Binding.StringFormat 仅在绑定到时使用String 类型的属性。

发生的情况是:

  1. Binding 正在对您的 MaxLevelOfInvestment 值进行装箱,并将其作为装箱的十进制值存储在 Label.Content 属性中。
  2. Label 控件有一个包含 ContentPresenter 的模板。
  3. 由于未设置 ContentTemplateContentPresenter 会查找为 Decimal 类型定义的 DataTemplate。当它找不到时,它会使用默认模板。
  4. ContentPresenter 使用的默认模板通过使用标签的 ContentStringFormat 属性来呈现字符串。

有两种可能的解决方案:

  • 使用 Label.ContentStringFormat 而不是 Binding.StringFormat,或者
  • 使用 String 属性(例如 TextBlock.Text)而不是 Label.Content

以下是如何使用 Label.ContentStringFormat:

<Label Content="{Binding Path=MaxLevelofInvestment}" ContentStringFormat="Amount is {0}" />

以下是如何使用 TextBlock:

<TextBlock Text="{Binding Path=MaxLevelofInvestment, StringFormat='Amount is {0}'}" />

注意:对于简单性我在上面的解释中省略了一个细节:ContentPresenter实际上使用它自己的TemplateStringFormat属性,但在加载过程中这些属性会自动模板化 -绑定到 LabelContentTemplateContentStringFormat 属性,因此看起来 ContentPresenter 实际上正在使用标签的属性。

The reason this doesn't work is that the Label.Content property is of type Object, and Binding.StringFormat is only used when binding to a property of type String.

What is happening is:

  1. The Binding is boxing your MaxLevelOfInvestment value and storing it the Label.Content property as a boxed decimal value.
  2. The Label control has a template that includes a ContentPresenter.
  3. Since ContentTemplate is not set, ContentPresenter looks for a DataTemplate defined for the Decimal type. When it finds none, it uses a default template.
  4. The default template used by the ContentPresenter presents strings by using the label's ContentStringFormat property.

Two solutions are possible:

  • Use Label.ContentStringFormat instead of Binding.StringFormat, or
  • Use a String property such as TextBlock.Text instead of Label.Content

Here is how to use Label.ContentStringFormat:

<Label Content="{Binding Path=MaxLevelofInvestment}" ContentStringFormat="Amount is {0}" />

Here is how to use a TextBlock:

<TextBlock Text="{Binding Path=MaxLevelofInvestment, StringFormat='Amount is {0}'}" />

Note: For simplicity I omitted one detail in the above explanation: The ContentPresenter actually uses its own Template and StringFormat properties, but during loading these are automatically template-bound to the ContentTemplate and ContentStringFormat properties of the Label, so it seems as if the ContentPresenter is actually using the Label's properties.

演出会有结束 2024-10-09 23:39:43

制作一个通用的StringFormatConverter : IValueConverter。将格式字符串作为 ConverterParameter 传递。

Label Content="{Binding Amount, Converter={...myConverter}, ConverterParameter='Amount is {0}'"

另外,当您在格式字符串中需要多个对象时,请使用 StringFormatMultiConverter : IMultiValueConverter,例如,Completed {0}tasks out of {1}

Make a universal StringFormatConverter : IValueConverter. Pass your format string as ConverterParameter.

Label Content="{Binding Amount, Converter={...myConverter}, ConverterParameter='Amount is {0}'"

Also, make StringFormatMultiConverter : IMultiValueConverter when you need more than one object in format string, for instance, Completed {0} tasks out of {1}.

装迷糊 2024-10-09 23:39:43

我刚刚检查过,由于某种原因它不能与 Label 一起使用,可能是因为它在内部使用 ContentPresenter 作为 Content 属性。您可以使用 TextBlock 来代替,这样就可以了。如果您需要继承样式、行为等,您还可以将下面的 TextBlock 摘录放在 Label 的内容中。

<TextBlock Text="{Binding Path=MaxLevelofInvestment, StringFormat='Amount is \{0\}'} />

I just checked and for some reason it doesn't work with the Label, probably because it uses a ContentPresenter for the Content property internally. You can use a TextBlock instead and that will work. You could also put the TextBlock excerpt below in the content of a Label if you need to inherit styling, behaviour etc.

<TextBlock Text="{Binding Path=MaxLevelofInvestment, StringFormat='Amount is \{0\}'} />
冰雪之触 2024-10-09 23:39:43

尝试使用转换器....

<myconverters:MyConverter x:Key="MyConverter"/>


<Label Content="{Binding Path=MaxLevelofInvestment, Converter={StaticResource MyConverter"} />


public class MyConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return String.Format("Amount is {0}", value);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value;
    }
}

Try using a converter....

<myconverters:MyConverter x:Key="MyConverter"/>


<Label Content="{Binding Path=MaxLevelofInvestment, Converter={StaticResource MyConverter"} />


public class MyConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return String.Format("Amount is {0}", value);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文