DataGrid DateTime IValueConverter,仅编辑时间,保留日期 [英] DataGrid DateTime IValueConverter, edit time only, preserve date

查看:66
本文介绍了DataGrid DateTime IValueConverter,仅编辑时间,保留日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在两列中分别显示DateTime作为Date和Time.使用输入"HHmmss"更新时间列会更新时间,但会将日期重置为当前日期.如何保存日期?

I want to display a DateTime in two columns as Date and Time respectively. Updating the time column with input "HHmmss" updates the time, but resets date to current date. How can the date be preserved?

public class TimeToStringConverter : IValueConverter
{
    public string Format { get; set; }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        Format = "HH:mm:ss";
        DateTime DateTimeValue = (DateTime)value;
        return DateTimeValue.ToString(Format);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string strValue = value.ToString();
        DateTime DateTimeValue;
        string format = "HHmmss";
        if (value.ToString().Length == 4)
            format = "HHmm";
        var res1 = DateTime.TryParseExact(strValue, format, null, DateTimeStyles.None, out DateTimeValue);
        if (res1)
            return DateTimeValue;
        return value;
    }
}

xaml:

<UserControl.Resources> 
    <valrule:TimeToStringConverter x:Key="timeConverter"/>
</UserControl.Resources>


<DataGridTextColumn Header="Time" MinWidth="50">
    <DataGridTextColumn.Binding>
        <Binding Path="Time" StringFormat="HH:mm:ss" UpdateSourceTrigger="Default"
                Converter="{StaticResource timeConverter}"  >
            <Binding.ValidationRules>
                <valrule:DateValidation/>
            </Binding.ValidationRules>
        </Binding>
    </DataGridTextColumn.Binding>
</DataGridTextColumn>

推荐答案

如果您不想像@Nawed Nabi Zada所说的那样将时间和日期分成两个属性,我还有另一种解决方案,它有点怪异.您可以将原始日期保存在转换器中.请注意,这仅在仅在一个地方使用转换器的情况下有效.

If you do not want to split the time and date in two properties as @Nawed Nabi Zada said, I have another solution which is a little bit hacky. You can save the original date in the converter. Note that this only works if you use the converter in only one place.

public class TimeToStringConverter : IValueConverter
{
    private DateTime _originaldate;
    public string Format { get; set; }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        Format = "HH:mm:ss";
        DateTime DateTimeValue = (DateTime)value;
        _originaldate = (DateTime)value;
        return DateTimeValue.ToString(Format);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string strValue = value.ToString();
        DateTime DateTimeValue;
        string format = "HH:mm:ss";
        if (value.ToString().Length == 4) format = "HH:mm";
        var res1 = DateTime.TryParseExact(strValue, format, null, DateTimeStyles.None, out DateTimeValue);
        if (res1)
        {
            DateTimeValue = _originaldate.Date + DateTimeValue.TimeOfDay;
            return DateTimeValue;
        }
        return value;
    }
}

这篇关于DataGrid DateTime IValueConverter,仅编辑时间,保留日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆