Tar*_*ras 2 wpf binding string-formatting
请问,有些人可以告诉我如何从代码隐藏中获取格式为"0.0"的双值,如下所示:
Binding b = new Binding(DoubleValue);
b.StringFormat = "????";
Run Code Online (Sandbox Code Playgroud)
在xaml中,它就像"0.0"一样...
那这个呢?
b.StringFormat = "{0:F1}";
Run Code Online (Sandbox Code Playgroud)
请参阅StringFormat的文档以及标准数字格式字符串和自定义数字格式字符串.
编辑:只是为了弄清楚如何在代码中创建和分配绑定(对于Text一个虚构的TextBlock 的属性textBlock):
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);
Run Code Online (Sandbox Code Playgroud)
或者:
var binding = new Binding
{
Path = new PropertyPath("DoubleValue"),
StringFormat = "{0:F1}"
};
textBlock.DataContext = viewModel;
textBlock.SetBinding(TextBlock.TextProperty, binding);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10910 次 |
| 最近记录: |