DataGrid DateTime IValueConverter, edit time only, preserve date

左心房为你撑大大i 提交于 2019-12-13 04:17:39

问题


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>

回答1:


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;
    }
}


来源:https://stackoverflow.com/questions/54941186/datagrid-datetime-ivalueconverter-edit-time-only-preserve-date

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!