Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 日期时间格式_Wpf_Datetime_Formatting - Fatal编程技术网

Wpf 日期时间格式

Wpf 日期时间格式,wpf,datetime,formatting,Wpf,Datetime,Formatting,我需要格式化日期,因此我使用了以下格式: <xcdg:Column Title="Registratiedatum" FieldName="RegistratieDatum" Width="1*"> <xcdg:Column.CellContentTemplate> <DataTemplate> <TextBlock Text="{Binding StringF

我需要格式化日期,因此我使用了以下格式:

        <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


你知道如何仅对“NOTNULL”值限制格式吗?抱歉,如果这可能是一个太基本的问题,但我仍处于学习的初级阶段。

A
Struct
作为一个
值类型
,它永远不能为
null

但是,有几种方法可以解决问题:

  • 在我看来,最干净、最合乎逻辑的方法是将日期时间更改为可为空的

  • 如果这不是一个选项,转换器将执行以下操作:

    .xaml代码:

    <UserControl.Resources>
       <local:DateConverter x:Key="dateConverter"/>
    </UserControl.Resources>         
    
    <TextBlock Text="{Binding MyDate, Converter={StaticResource dateConverter}}" />
    
  • 最后,Xaml代码中的DataTrigger允许您在日期为null时隐藏/折叠控件。在转换器中,关键是检查日期何时等于
    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>
    
    
    

  • 它们真的是空的吗?DateTime是
    struct
    ,初始值正好是
    01/01/0001 00:00:00
    。因为DateTime是一个结构,所以它不能为空。除此之外,您可以使用绑定转换器而不是StringFormat。在数据库中,它显示如下:列:[DAT\u EERSTE\u VERWERKING](datetime,null),并且一些值为null@克莱门斯:我也在考虑使用转换器(到目前为止从未使用过),但希望有一个更简单的解决方案。无论如何,谢谢你们的快速回复,伙计们!您的属性可能会生成为
    null
    DateTime?
    。无论如何,转换器应该在这里完成这项工作。你可以使用一个自定义的格式化程序,为空值返回null。非常感谢!你提供的各种选择给我留下了深刻的印象。在我的例子中,因为文本框实际上是xcdg Datagrid中的一个字段,所以选择了选项2——正如预期的那样工作。
    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();
      }
    }
    
    <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>