TextBlock与格式化字符串绑定 [英] TextBlock binding with formatted string

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

问题描述

我正在使用TexBlock来显示一些有界数据'Value1'。


如何在Value1之后添加一些静态文本????? / p>

所以如果Value1 =" 1234567"


我想在之后添加一些静态文本'(以毫秒为单位)'


所以最终结果是   1234567(以毫秒为单位的时间)


我最初的猜测是使用StringFormat,但我是菜鸟,以前从未使用它。有没有一个好的资源显示我如何展示?另外,如果我遇到问题,StringFormat实际上会是什么。非常感谢!


 

< TextBlock Grid.Column =" 3" Grid.Row = QUOT; 0" Style =" {DynamicResource TextValueStyle}" Text =" {Binding Path = Value1}" /> 

解决方案

您可以使用IValueConverter。

 
public class TextValueConverter:IValueConverter
{
#region IValueConverter成员

public object 转换( object 值,Type targetType, object 参数,System.Globalization.CultureInfo文化)
{
return String.Format (" {0}({1})" ,value,DateTime.Now.TimeOfDay.TotalMilliseconds());
}

public object ConvertBack( object 值,类型targetType, object 参数,System.Globalization.CultureInfo文化)
{
throw new NotImplementedException();
}

#endregion
}


I'm using a TexBlock to display some bounded data 'Value1'.

How do I add some static text to be displayed after the Value1????

so if Value1 = "1234567"

I want to add some static text afterwards '(time in milliseconds)'

So the end result is   1234567 (time in milliseconds)

My initial guess is to use a StringFormat, but I am rookie and am never used it before. Is there a resource which is good which shows how I can display? Also what would the StringFormat actually be in case I run into problems. Thank you very much!

 

 <TextBlock Grid.Column="3" Grid.Row="0" Style="{DynamicResource TextValueStyle}" Text="{Binding Path=Value1}" />

解决方案

You could use an IValueConverter.

  public class TextValueConverter : IValueConverter
  {
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
      return String.Format("{0} ({1})", value, DateTime.Now.TimeOfDay.TotalMilliseconds());
    }

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

    #endregion
  }


这篇关于TextBlock与格式化字符串绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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