标签内容上的 WPF StringFormat

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

WPF StringFormat on Label Content

wpfwpf-controlsbindingwpftoolkitwpfdatagrid

提问by Everything Matters

I want to format my string binding as Amount is Xwhere Xis a property bound to a label.

我想我的格式字符串作为结合Amount is X哪里X是绑定到一个标签的属性。

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.

我什至尝试将绑定属性的数据类型更改为int,stringdouble。似乎没有任何效果。这是一个非常常见的用例,但似乎不受支持。

回答by Ray Burns

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

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

What is happening is:

正在发生的事情是:

  1. The Bindingis boxing your MaxLevelOfInvestmentvalue and storing it the Label.Contentproperty as a boxed decimal value.
  2. The Label control has a template that includes a ContentPresenter.
  3. Since ContentTemplateis not set, ContentPresenterlooks for a DataTemplatedefined for the Decimaltype. When it finds none, it uses a default template.
  4. The default template used by the ContentPresenterpresents strings by using the label's ContentStringFormatproperty.
  1. Binding是拳击的MaxLevelOfInvestment价值并将其存储的Label.Content属性作为盒装十进制值。
  2. Label 控件有一个模板,其中包含一个ContentPresenter.
  3. 由于ContentTemplate未设置,ContentPresenter查找DataTemplate定义的Decimal类型。当它没有找到时,它使用默认模板。
  4. ContentPresenter通过使用标签的ContentStringFormat属性呈现字符串使用的默认模板。

Two solutions are possible:

有两种解决方案:

  • Use Label.ContentStringFormat instead of Binding.StringFormat, or
  • Use a String property such as TextBlock.Text instead of Label.Content
  • 使用 Label.ContentStringFormat 而不是 Binding.StringFormat,或
  • 使用 String 属性,例如 TextBlock.Text 而不是 Label.Content

Here is how to use Label.ContentStringFormat:

下面是如何使用 Label.ContentStringFormat:

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

Here is how to use a TextBlock:

下面是如何使用 TextBlock:

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

Note: For simplicity I omitted one detail in the above explanation: The ContentPresenteractually uses its own Templateand StringFormatproperties, but during loading these are automatically template-bound to the ContentTemplateand ContentStringFormatproperties of the Label, so it seems as if the ContentPresenteris actually using the Label's properties.

注意:为了简单起见,我在上面的解释中省略了一个细节:ContentPresenter实际上使用了自己的TemplateStringFormat属性,但是在加载过程中,这些自动模板绑定到 的ContentTemplateContentStringFormat属性Label,因此看起来好像ContentPresenter实际上是在使用Label的属性.

回答by Michael Agroskin

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

制作一个通用的StringFormatConverter : IValueConverter. 将您的格式字符串作为ConverterParameter.

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

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

此外,StringFormatMultiConverter : IMultiValueConverter当您需要格式字符串中的多个对象时,例如,Completed {0} tasks out of {1}.

回答by Guy

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.

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

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

回答by Gabe

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;
    }
}

回答by strattonn

Maybe this will help...

也许这会有所帮助...

Embed code in XAML

在 XAML 中嵌入代码