从code-背后绑定字符串格式? [英] binding string format from code-behind?

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

问题描述

请,可一些身体告诉我怎么把我的双重价值格式,如0.0从code-背后,是这样的:

Please, can some body tell me how to get my double value formatted like "0.0" from code-behind, like this:

Binding b = new Binding(DoubleValue);
b.StringFormat = "????";

在XAML它的工作原理就像是0.0...

In xaml it works just like that "0.0"...

推荐答案

这个是什么?

b.StringFormat = "{0:F1}";


请参阅的StringFormat 也标准数字格式字符串并的自定义数字格式字符串的。

编辑:只是为了明确如何结合将创建并分配(名为假想TextBlock的文本属性正文块

Just to make clear how a binding would be created and assigned (to the Text property of an imaginary TextBlock named textBlock) in code:

public class ViewModel
{
    public double DoubleValue { get; set; }
}

...

var viewModel = new ViewModel
{
    DoubleValue = Math.PI
};

var binding = new Binding
{
    Source = viewModel,
    Path = new PropertyPath("DoubleValue"),
    StringFormat = "{0:F1}"
};

textBlock.SetBinding(TextBlock.TextProperty, binding);

或者

var binding = new Binding
{
    Path = new PropertyPath("DoubleValue"),
    StringFormat = "{0:F1}"
};

textBlock.DataContext = viewModel;
textBlock.SetBinding(TextBlock.TextProperty, binding);

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

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