文本框的自定义日期格式 [英] Custom date format for textbox

查看:132
本文介绍了文本框的自定义日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相关:将3个文本框绑定在一起;相同的DateTime格式

我有三个文本框,应该都与同一日期。其中两个具有正常的字符串格式。第三个格式为 yyyy,jjj / HHmmss 。我不知道如何将该文本框绑定到我拥有的自定义格式,并且如果我更改其中的任何日期值,则将其绑定为其他<$

I have three textboxes, all are supposed to be bound together with the same date. Two of them have normal string formats. The third one has a specific format of yyyy,jjj/HHmmss. I can't figure out how to bind this textbox to the custom format I have, and make it so if I change any of the date values in it, the other textboxes will update and vice versa.

private DateTime _dateInViewModel;
public DateTime DateInViewModel
{
    get { return _dateInViewModel; }
    set
    {
        _dateInViewModel = value;
        NotifyPropertyChanged("DateInViewModel");
    }
}

<TextBox Name="SDate1" Text="{Binding DateInViewModel, StringFormat='MM/dd/yyyy'}" />
<TextBox Name="SDate2" Text="{Binding DateInViewModel}" />
<TextBox Name="STime1" Text="{Binding DateInViewModel, StringFormat='hh:mm:ss'}" />

自定义格式可以如下所示:

The custom format can be made like:

format = String.Format("{0},{1}/{2}",
                                DateInViewModel.Year,
                                DateInViewModel.DayOfYear.ToString("d3"),
                                DateInViewModel.ToString("HHmmss"));

现在,仅 SDate1 STime1 相互正确绑定,并在其他更改时更新。

Right now, only SDate1 and STime1 bind to each other properly and update when the other is changed.

我做了一个转换器。当更改 SDate1 STime1 时,它会正确更新 SDate2 ,但在编辑 SDate2 以更新其他版本时不起作用。

I made a converter. It properly updates SDate2 when SDate1 and STime1 are changed, but doesn't work when editing SDate2 to update the others.

public class DateTimeConverter : IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value != null)
        {
            DateTime test = (DateTime)value;
            string date = String.Format("{0},{1}/{2}",
                                            test.Year,
                                            test.DayOfYear.ToString("d3"),
                                            test.ToString("HHmmss"));
            return (date);
        }
        return string.Empty;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    #endregion
}

推荐答案

您需要在转换器中重新设置转换。这只是一个示例,但是您需要将值解析回原始源,以便可以更新其他绑定。

You need to set the convert back in the converter. This is just an example but you need to parse the value back into original source so other binds can be updated.

因为您的格式是 {0 },{1} / {2} ,则需要将其拆分回去并重建预期的日期。

since your format is {0},{1}/{2} then you need to split it back up and reconstruct the intended date.

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
    if (value == null) return null;
    string strValue = value.ToString();
    if (string.IsNullOrEmpty(strValue) && targetType == typeof(DateTime?))
    {
        return null;
    }
    else if (string.IsNullOrEmpty(strValue))
    {
        return DateTime.MinValue;
    }

    //year,dayOfYear/Time(HHmmss)
    var parts = strValue.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
    if (parts.Length == 2) {
        var year = parts[0];
        parts = parts[1].Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
        if (parts.Length == 2) {
            var days = parts[0];
            var time = parts[1];

            var date = new DateTime(int.Parse(year), 1, 1)
                            .AddDays(int.Parse(days))
                            .Add(TimeSpan.Parse(time));
            return date;
        }
    }

    DateTime resultDateTime;
    return DateTime.TryParse(strValue, out resultDateTime) ? resultDateTime : value;
}

这篇关于文本框的自定义日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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