0

我有一个绑定到 DateTime 的 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);
            }
        }
    }

从我的 XAML

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

2 回答 2

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

对我来说很好。记住ContentStringFormat_Label

于 2013-05-30T15:57:51.143 回答
1

您可以编写一个自定义的“IValueConverter”类,按照您希望的方式格式化日期。

于 2013-05-30T15:52:27.417 回答