wpf 仅绑定标签的一部分

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

Binding only part of a label

wpfbindinglabel

提问by Neil Barnwell

How would one achieve mixing bound values with constant text in a WPF bound control?

如何在 WPF 绑定控件中实现将绑定值与常量文本混合?

For example, say I have a form displaying orders, and I want a label that displays text like "Order ID 1234".

例如,假设我有一个显示订单的表单,我想要一个显示“订单 ID 1234”等文本的标签。

I've tried things like:

我试过这样的事情:

text="Order ID {Binding ....}"

Is this achievable, or do I have to do something like having more than one label in a flow control?

这是可以实现的,还是我必须做一些事情,比如在流控制中拥有多个标签?

采纳答案by Inferis

If you're using 3.5 SP1, you can use the StringFormatproperty on the binding:

如果您使用的是 3.5 SP1,则可以使用StringFormat绑定上的属性:

<Label Content="{Binding Order.ID, StringFormat=Order ID \{0\}}"/>

Otherwise, use a converter:

否则,请使用转换器:

<local:StringFormatConverter x:Key="StringFormatter" StringFormat="Order ID {0}" />
<Label Content="{Binding Order.ID, Converter=StringFormatter}"/>

With StringFormatConverterbeing an IValueConverter:

随着StringFormatConverter作为一个IValueConverter

[ValueConversion(typeof(object), typeof(string))]
public class StringFormatConverter : IValueConverter
{
    public string StringFormat { get; set; }

    public object Convert(object value, Type targetType,
                          object parameter, CultureInfo culture) {
         if (string.IsNullOrEmpty(StringFormat)) return "";
         return string.Format(StringFormat, value);
    }


    public object ConvertBack(object value, Type targetType,
                              object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }

That'll do the trick.

这样就行了。

[Edit: Change the Textproperty to Content]

[编辑:将Text属性更改为Content]

回答by LPCRoy

The Binding.StringFormat property doesn't work on Labels, you need to use the ContentStringFormat property on the Label.
For example, the following sample will work:

Binding.StringFormat 属性对标签不起作用,您需要在标签上使用 ContentStringFormat 属性。
例如,以下示例将起作用:

<Label>
    <Label.Content>
        <Binding Path="QuestionnaireName"/>
    </Label.Content>
    <Label.ContentStringFormat>
        Thank you for taking the {0} questionnaire
    </Label.ContentStringFormat>
</Label> 

The same as short Version:

与短版相同:

<Label Content="{Binding QuestionnaireName}" ContentStringFormat="Thank you for taking the {0} questionnaire" />

Using it to display a unit after the value:

使用它在值后显示一个单位:

<Label Content="{Binding Temperature}" ContentStringFormat="{}{0}°C" />

While this sample will not:

虽然此示例不会:

<Label>
    <Label.Content>
        <Binding Path="QuestionnaireName" StringFormat="Thank you for taking the {0} questionnaire"/>
    </Label.Content>            
</Label>

回答by bendewey

Often overlooked is simply chaining multiple textblocks together for example

例如,经常被忽视的是简单地将多个文本块链接在一起

<TextBlock Text="{Binding FirstName}" />
<TextBlock Text=" " />
<TextBlock Text="{Binding LastName}" />

回答by Rob Walker

Another approach is to use a single TextBlock with multiple Run elements within it:

另一种方法是使用包含多个 Run 元素的单个 TextBlock:

<TextBlock><Run>Hello</Run><Run>World</Run></TextBlock>

.. but to bind to a element you need to use add a BindableRunclass.

.. 但是要绑定到一个元素,你需要使用添加一个BindableRun类。

UpdateBut there are some drawbacks to this technique ... see here

更新但是这种技术有一些缺点……见这里

回答by Miko?aj

I found another one approach. @Inferis's solution doesn't work for me and @LPCRoy's isn't elegant for me:

我找到了另一种方法。@Inferis 的解决方案对我不起作用,@LPCRoy 的解决方案对我来说并不优雅:

<Label Content="{Binding Path=Order.ID, FallbackValue=Placeholder}" ContentStringFormat="Order ID {0}">

It's my favourite at this moment, it's seems flexible and condensed.

这是我此刻最喜欢的,它看起来灵活而凝练。

回答by Jogi Joseph George

Modified Mikolaj's answer.

修改了 Mikolaj 的答案。

<Label Content="{Binding Order.ID}" ContentStringFormat="Order ID {0}" />

FallbackValue is not a must.

FallbackValue 不是必须的。