WPF中的日期时间和数据绑定-前导零和空白标签

本文关键字:标签 空白 数据绑定 日期 时间 WPF | 更新日期: 2023-09-27 18:16:19

我有一个WPF标签绑定到一个日期时间,我需要它在1/1/1开始。当设置为1/1/1时,标签根本不出现。如果我更改任何数字,它会在年份(1/2/0001)前面显示前导零。在我使用WinForms之前,所有这些都工作得很好。

有没有人知道如何让标签显示时设置为1/1/1和/或如何取消前导零?

另外,既然我想使用虚构的时间,有没有其他更合适的方法可以尝试?

DateTime _date = new DateTime(1, 1, 1);
public DateTime Date
    {
        get { return _date; }
        set
        {
            if (_date != value)
            {
                _date = value;
                RaisePropertyChanged(() => Date);
            }
        }
    }

From my XAML

Label Content="{Binding Date}" Style="{DynamicResource SimpleLabel}" FontFamily="Pericles" FontSize="16" VerticalAlignment="Top" HorizontalAlignment="Left"/

WPF中的日期时间和数据绑定-前导零和空白标签

<TextBlock Text="{Binding Date, StringFormat='{0:d'/M'/y'}}" />
<Label Content="{Binding Date}"
        ContentStringFormat="{}{0:d'/M'/y}" />

适合我。记住LabelContentStringFormat

您可以编写一个自定义的'IValueConverter '类,以您想要的方式格式化日期。