且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从code-背后绑定字符串格式?

更新时间:2023-09-26 11:37:04

这个是什么?

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


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

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

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);