【问题标题】:WPF DateTime FormattingWPF 日期时间格式
【发布时间】:2016-11-06 06:51:08
【问题描述】:

我需要格式化日期,所以我使用了以下内容:

        <xcdg:Column Title="Registratiedatum" FieldName="RegistratieDatum" Width="1*">
        <xcdg:Column.CellContentTemplate>
            <DataTemplate>
                   <TextBlock Text="{Binding StringFormat='{}{0:dd/MM/yyyy HH:mm:ss }', TargetNullValue={x:Static System:String.Empty}}" />
                </DataTemplate>
        </xcdg:Column.CellContentTemplate>
        </xcdg:Column>

但是,有些日期是空的,所以我希望这些字段保持为空,但我猜由于格式它看起来像这样:

01/01/0001 00:00:00

知道如何限制“非空”值的格式吗?对不起,如果这可能是太基本的问题,但我仍处于学习的初级阶段。

【问题讨论】:

  • 他们真的是null吗? DateTime 是struct,初始值正好是01/01/0001 00:00:00。
  • 由于 DateTime 是一个结构,它不能为空。除此之外,您可以使用绑定转换器而不是 StringFormat。
  • 在数据库中显示如下:column: [DAT_EERSTE_VERWERKING](datetime,null) 并且一些值为NULL。 @Clemens:我也在考虑使用转换器(到目前为止从未使用过),但希望可能有一个更简单的解决方案。无论如何,谢谢你们的快速回复,伙计们!
  • 您的属性可能生成为Nullable&lt;DateTime&gt; 或DateTime?。无论如何,转换器应该在这里完成工作。
  • 您可以使用自定义格式化程序,为空值返回 null stackoverflow.com/questions/7689040/…

标签: wpf datetime formatting


【解决方案1】:

Struct 是 value type,它永远不可能是 null。

不过,有几种方法可以解决您的问题:

  1. 在我看来,最简洁和最合乎逻辑的方法是将您的 DateTime 更改为 Nullable&lt;Datetime&gt;

    private DateTime? myDate;
    public DateTime? MyDate
    {
         get
         {
            return this.myDate;
         }
         set
         {
            this.myDate = value;
         }
    }
    
  2. 如果这不是一个选项,转换器可以解决问题:

    .xaml 代码:

    <UserControl.Resources>
       <local:DateConverter x:Key="dateConverter"/>
    </UserControl.Resources>         
    
    <TextBlock Text="{Binding MyDate, Converter={StaticResource dateConverter}}" />
    

    .cs代码

    public class DateConverter: IValueConverter
    {
      public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
      {
         if (value != null)
         {
            DateTime myDate = (DateTime)value;
            if (myDate != DateTime.MinValue)
            {
               return myDate.ToString("dd/MM/yyyy HH:mm:ss");
            }
         }
    
         return String.Empty;
      }
    
      public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
      {
         throw new NotImplementedException();
      }
    }
    
  3. 最后,直接在 Xaml 代码中的 DataTrigger 允许您在日期为空时隐藏/折叠控件。与转换器一样,关键是检查日期何时等于DateTime.MinValue。

    <UserControl x:Class="WpfApplicationTest.MyUserControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:sys="clr-namespace:System;assembly=mscorlib"
         xmlns:local="clr-namespace:WpfApplicationTest"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
    
         <TextBlock Text="{Binding MyDate, StringFormat='{}{0:dd/MM/yyyy HH:mm:ss }'}" >
            <TextBlock.Style>
               <Style TargetType="TextBlock">
                     <Style.Triggers>
                        <DataTrigger Binding="{Binding MyDate}" Value="{x:Static sys:DateTime.MinValue}">
                           <Setter Property="Visibility" Value="Collapsed"></Setter>
                        </DataTrigger>
                     </Style.Triggers>
                  </Style>
               </TextBlock.Style>
         </TextBlock>
    

【讨论】:

  • 非常感谢!您提供的各种选择给我留下了深刻的印象。在我的例子中,由于 TextBox 实际上是 xcdg Datagrid 中的一个字段,因此选择了选项 2 - 就像预期的那样工作。
猜你喜欢
  • 2018-03-21
  • 1970-01-01
  • 1970-01-01
  • 2013-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多