PriorityBinding - 对标签中的每个绑定使用不同的 StringFormat [英] PriorityBinding - Use different StringFormat for each binding in a Label

查看:22
本文介绍了PriorityBinding - 对标签中的每个绑定使用不同的 StringFormat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 WPF 项目中,我有一个 Label:

In my WPF project, I have a Label:

<Label Grid.Column="1"
       Grid.Row="1">
    <PriorityBinding>
        <Binding Path="Worker.Employer.Name" StringFormat="Employer: {0}" />
        <Binding Source="Unemployed" />
    </PriorityBinding>
</Label>

StringFormat 似乎没有做任何事情.但是,如果我添加这个:

The StringFormat on doesn't seem to do anything. However, if I add this:

<Label.ContentStringFormat>
    Employer: {0}
</Label.ContentStringFormat>

... 格式有效,但它会影响两个绑定.如何将 StringFormat 仅应用于顶部绑定?

... the formatting works, but it affects both bindings. How can I apply the StringFormat to only the top binding?

更新:这么短的使用 TextBlock 而不是 Label,有什么办法可以做到这一点吗?

Update: So short of using a TextBlock instead of a Label, is there any way to accomplish this?

推荐答案

如前所述,StringFormat 仅在目标属性类型为文本时有效.

As already explained StringFormat only works when the target property type is text.

一种解决方案是使用 ValueConverter 来格式化结果,您可以将格式字符串作为 ConverterParameter 传入.

One solution would be to use a ValueConverter to format the result, you can pass in the format string as the ConverterParameter.

未能创建字符串类型的附加 DependencyProperty

Failing that create an attached DependencyProperty of type string

public static class Helper {
    public static readonly DependencyProperty TextProperty = DependencyProperty.RegisterAttached("Text", typeof(string), typeof(Helper));

   public static string GetText(DependencyObject o) {
     return (string)o.GetValue(TextProperty);
   }

   public static void SetText(DependencyObject o, string value) {
      o.SetValue(TextProperty,value);
   }
}

然后你就可以了

<Label Grid.Column="1"
       Grid.Row="1" 
       Content="{Binding RelativeSource={RelativeSource Self}, Path=(ui:Helper.Text)}">
    <ui:Helper.Text>
    <PriorityBinding>
        <Binding Path="Worker.Employer.Name" StringFormat="Employer: {0}" />
        <Binding Source="Unemployed" />
    </PriorityBinding>
    </ui:Helper.Text>
</Label>

评论中描述的问题可能与这个问题和所以 XAML 可能需要像这样.

The problem describe in the comments may be related to This Question and so the XAML might need to look like this.

<Label Grid.Column="1"
       Grid.Row="1" >
       <Binding RelativeSource="{RelativeSource Self}"  Path="(ui:Helper.Text)" />
      <ui:Helper.Text>
      <PriorityBinding>
          <Binding Path="Worker.Employer.Name" StringFormat="Employer: {0}" />
          <Binding Source="Unemployed" />
      </PriorityBinding>
      </ui:Helper.Text>
</Label>

或来自 这个 MSDN 问题 你可以做

<Label Grid.Column="1"
       Grid.Row="1" >
      <ui:Helper.Text>
      <PriorityBinding>
          <Binding Path="Worker.Employer.Name" StringFormat="Employer: {0}" />
          <Binding Source="Unemployed" />
      </PriorityBinding>
      </ui:Helper.Text>
      <Label.Content>
         <Binding RelativeSource="{RelativeSource Self}"  Path="(ui:Helper.Text)" />
      </Label.Content>
    </Label>

确保将 ui 的 xmlns 绑定到 Helper 类的命名空间

Make sure that you bind the xmlns for ui to the namespace of your Helper class

您始终可以将 Content 相对源绑定放入一个样式中,以避免对所有标签重复该绑定.

You could always put the Content relative source binding into a style to avoid repeating it for all labels.

这篇关于PriorityBinding - 对标签中的每个绑定使用不同的 StringFormat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆