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
但是,有几种方法可以解决问题:
<UserControl.Resources>
<local:DateConverter x:Key="dateConverter"/>
</UserControl.Resources>
<TextBlock Text="{Binding MyDate, Converter={StaticResource dateConverter}}" />
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>