TextBlock的:文本和的StringFormat绑定 [英] TextBlock: Binding of Text and StringFormat

查看:277
本文介绍了TextBlock的:文本和的StringFormat绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以绑定文本的StringFormat 吗?

<TextBlock Text="{Binding Path=Price, StringFormat={Binding Path=DecimalPoints}}" />

DecimalPoints 的是不断从 F0 更改为 F15 。可惜的是在$ C $以上C不编译。

DecimalPoints is constantly changing from F0 to F15. Unfortunatelly the code above doesn't compile.

推荐答案

我觉得你最好的选择绝对是一个转换器。然后你的绑定是这样的:

I think your best bet is definitely a converter. Then your binding would look like this:

<TextBlock.Text>
   <MultiBinding Converter="{StaticResource StringFormatConverter }">
      <Binding Path="Price"/>
      <Binding Path="DecimalPoints"/>
   </MultiBinding>
</TextBlock.Text>

然后快速转换器(你当然可以让它更好,但这是一般的想法)。

Then a quick converter (you can certainly make it nicer, but this is the general idea).

    public class StringFormatConverter : IMultiValueConverter
    {
      #region IMultiValueConverter Members

      public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
          double number = (double)values[0];
          string format = "f" + ((int)values[1]).ToString();
          return number.ToString(format);
      }

      public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
      {
        throw new NotImplementedException();
      }

      #endregion
    }

这篇关于TextBlock的:文本和的StringFormat绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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