C# 在DataGrid中使用WPF格式化日期

C# 在DataGrid中使用WPF格式化日期,c#,wpf,datetime,C#,Wpf,Datetime,我有一个DataGrid,它有一个开始时间列。它绑定到一个字符串字段,该字段以hh:mm:ss格式保存时间。但是,我只希望以hh:mm格式显示网格中的时间。有没有办法在绑定中使用字符串格式属性来实现这一点 像这样: <DataGridTextColumn Binding="{Binding MyDate, StringFormat='HH:mm'}" /> 像这样: <DataGridTextColumn Binding="{Binding MyDate, StringFo

我有一个DataGrid,它有一个开始时间列。它绑定到一个字符串字段,该字段以hh:mm:ss格式保存时间。但是,我只希望以hh:mm格式显示网格中的时间。有没有办法在绑定中使用字符串格式属性来实现这一点

像这样:

<DataGridTextColumn Binding="{Binding MyDate, StringFormat='HH:mm'}" />

像这样:

<DataGridTextColumn Binding="{Binding MyDate, StringFormat='HH:mm'}" />

我已经搜索完了,试图找出WPF中绑定的StringFormat属性是否有办法将字符串转换为日期,然后将其格式化为HH:mm

因此,我已经开始创建一个转换器,以实现上述建议的相同功能。我想我会把它贴在这里供有这种需要的人使用

public class StringToDateConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null)
        {
            return string.Empty;
        }
        DateTime d;
        try
        {
            d = System.Convert.ToDateTime(value);
            return d;
            // The WPF code that uses this converter will then use the stringformat 
            // attribute in binding to display just HH:mm part of the date as required.
        }
        catch (Exception)
        {
            // If we're unable to convert the value, then best send the value as is to the UI.
            return value;
        }

    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        //Insert your implementation here...
        return value;
    }
}
下面是如何在WPF中实现结果

    <DataGridTextColumn Header="Start Time"  Width="Auto" Binding="{Binding VisitOpStartTime, Converter={StaticResource s2dConverter}, StringFormat=\{0:HH:mm\}}"></DataGridTextColumn>

最后,别忘了添加以下内容,当然是根据您的需要定制的

    xmlns:converter="clr-namespace:[YourAppNamespace].Converters"

    <Window.Resources>
        <converter:StringToDateConverter x:Key="s2dConverter" />
    </Window.Resources>
xmlns:converter=“clr命名空间:[YourAppNamespace]。转换器”

希望这能有所帮助。

我已经搜索完了,试图找出WPF中绑定的StringFormat属性是否有办法将字符串转换为日期,然后将其格式化为HH:mm

因此,我已经开始创建一个转换器,以实现上述建议的相同功能。我想我会把它贴在这里供有这种需要的人使用

public class StringToDateConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null)
        {
            return string.Empty;
        }
        DateTime d;
        try
        {
            d = System.Convert.ToDateTime(value);
            return d;
            // The WPF code that uses this converter will then use the stringformat 
            // attribute in binding to display just HH:mm part of the date as required.
        }
        catch (Exception)
        {
            // If we're unable to convert the value, then best send the value as is to the UI.
            return value;
        }

    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        //Insert your implementation here...
        return value;
    }
}
下面是如何在WPF中实现结果

    <DataGridTextColumn Header="Start Time"  Width="Auto" Binding="{Binding VisitOpStartTime, Converter={StaticResource s2dConverter}, StringFormat=\{0:HH:mm\}}"></DataGridTextColumn>

最后,别忘了添加以下内容,当然是根据您的需要定制的

    xmlns:converter="clr-namespace:[YourAppNamespace].Converters"

    <Window.Resources>
        <converter:StringToDateConverter x:Key="s2dConverter" />
    </Window.Resources>
xmlns:converter=“clr命名空间:[YourAppNamespace]。转换器”

希望这有帮助。

它真的是
字符串字段吗?
,如果是,您需要带字符串操作的转换器。是的,它是字符串格式。我想知道stringformat是否有处理字符串数据类型的方法。无论如何,谢谢。没有转换器,这是不可能的。它真的是
字符串字段
?,如果是,你需要带字符串操作的转换器。是的,它是字符串格式。我想知道stringformat是否有处理字符串数据类型的方法。无论如何,谢谢。如果没有转换器,这是不可能的。我想如果数据是datetime类型的话,这就行了。我的是一个字符串。我想如果数据是datetime类型的话,这就行了。我的是一根绳子。