【发布时间】:2018-02-12 11:57:51
【问题描述】:
我有一个 WPF 项目,其中包含一个 TextBlock 并绑定到一个数字
喜欢"0009012" 但我只想显示9012 或0881012
如果必须显示881012。
有没有办法使用 StringFormat 或者我必须编写一个转换器 为此?
这是我的代码:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBlock Text="{Binding Test}" StringFormat={}{????}/>
</Grid>
</Window>
using System.Windows;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public string Test { get; set; }
public MainWindow()
{
Test = "00090012";
InitializeComponent();
DataContext = this;
}
}
}
【问题讨论】:
-
"00090012"不是数字。它是一个字符串。它按原样显示。 -
您可以试试
StringFormat={}{0:0}}"看看显示的内容吗? -
"StringFormat 有没有办法,还是我必须为此编写一个转换器?" 你必须编写一个转换器。
标签: c# wpf xaml binding string-formatting