如何将双 DateTimePicker 数据绑定到单个 DateTime 对象

发布于 2024-08-30 07:24:35 字数 658 浏览 8 评论 0原文

我有一个简单的表单,有两个 DateTimePicker 控件:一个用于日期,一个用于时间。问题是这两个控件应该代表一个时间点。因此,我想将它们“绑定”到表单上的单个 DateTime 属性(为了简单起见)。我做了以下操作:

// Start is a DateTime property on the form
_StartDate.DataBindings.Add("Value", this, "Start"); 
_StartTime.DataBindings.Add("Value", this, "Start");

但是挂钩“ValueChanged”事件会产生混合结果...有时我得到了我想要的,有时属性的更新“缓慢”。我认为这种分成两个 DateTimePicker 的方式相当常见。那么该怎么做呢?

更新:其中可能存在多个问题:

  1. 如何将 DateTimePicker(格式:日期)绑定到表单上的 DateTime 属性?
  2. 那么,如何将另一个 DateTimePicker(格式:时间)绑定到同一属性?
  3. 我正在使用 Visual Studio Express 2008 (.NET 3.5),并且我似乎在值更改之前从 DateTimePickers 获取 ValueChanged 事件?

I have a simple form, with two DateTimePicker-controls: One for date, and one for time. The thing is these two controls are supposed to represent a single point in time. Hence I would like to "Bind" them to a single DateTime property on my form (for simplicity). I did the following:

// Start is a DateTime property on the form
_StartDate.DataBindings.Add("Value", this, "Start"); 
_StartTime.DataBindings.Add("Value", this, "Start");

But hooking into "ValueChanged" event, yields mixed results... Sometimes I get exactly what I want, sometimes the updates of the property are "sluggish". I figured this way of splitting into two DateTimePicker's was fairly common. So how to do it?

Update: There is possibly multiple questions in there:

  1. How do I bind a DateTimePicker (Format: Date) to a DateTime Property on a form?
  2. Then, how do I bind yet another DateTimePicker (Format: Time) to the same property?
  3. I'm using Visual Studio Express 2008 (.NET 3.5), and I seemingly get ValueChanged events from the DateTimePickers before the value is changed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

北陌 2024-09-06 07:24:35

我发现自己有时会遇到这个问题,并且我使用了一种有点丑陋的技术。我没有直接绑定到该属性,而是在模型上创建了两个附加属性。

//The data I want to bind to
public DateTime Something { get; set; }
//Two new properties
public DateTime SomethingDate { /*...*/ }
public DateTime SomethingTime { /*...*/ }

每个的 get 部分应该只返回 Somethingset 部分是您实现一点逻辑的地方,以确保 Something 始终准确反映。

public DateTime SomethingDate
{
    get
    {
        return this.Something;
    }
    set
    {
        this.Something = value.Date.Add(this.Something.TimeOfDay);
    }
}
public DateTime SomethingTime
{
    get
    {
        return this.Something;
    }
    set
    {
        this.Something = this.Something.Date.Add(value.TimeOfDay);
    }
}

现在,您的 Something 属性将始终正确反映两个控件相应的日期和时间,同时仅使用我们关心的每个控件的信息更新值。

I find myself having this problem on occasion and I use a somewhat ugly technique. Instead of binding directly to the property I create two additional properties on the model.

//The data I want to bind to
public DateTime Something { get; set; }
//Two new properties
public DateTime SomethingDate { /*...*/ }
public DateTime SomethingTime { /*...*/ }

The get sections of each should merely return Something. The set sections is where you implement a tiny bit of logic to ensure Something is always reflected accurately.

public DateTime SomethingDate
{
    get
    {
        return this.Something;
    }
    set
    {
        this.Something = value.Date.Add(this.Something.TimeOfDay);
    }
}
public DateTime SomethingTime
{
    get
    {
        return this.Something;
    }
    set
    {
        this.Something = this.Something.Date.Add(value.TimeOfDay);
    }
}

Now your Something property will always properly reflect the corresponding date and times of the two controls while only updating the value with the piece of information from each that we care about.

葬﹪忆之殇 2024-09-06 07:24:35

不幸的是,我认为您可能会发现自己正在编写一个包含两者的自定义控件,您可以将数据绑定到该控件。这就是我们解决同样问题的方法。

Unfortunately, I think you may be finding yourself writing a custom control that includes both, which you can data bind to. That's how we solved the same problem.

擦肩而过的背影 2024-09-06 07:24:35

您不能直接将 DateTime 结构成员与绑定类绑定,
为此,您必须包装一个 DateTime 结构并实现 INotifyPropertyChanged 接口的 PropertyChanged 事件,如下所示:

public class DateTimeWrapper : INotifyPropertyChanged
{
    private DateTime _dateTime;
    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged()
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs("Value"));
    }

    public DateTime Value
    {
        get { return _dateTime; }
        set
        {
            _dateTime = value;
            OnPropertyChanged();
        }
    }
}

然后您可以使用像这样的 Bindig 对象:

var binding = new Binding("Value", _time, "Value", true);
binding.ControlUpdateMode = ControlUpdateMode.OnPropertyChanged;
dateTimePicker1.DataBindings.Add(binding);

You can't directly bind DateTime struct member with a Binding Class,
for do it you have to wrap a DateTime struct and Implement the PropertyChanged event of the INotifyPropertyChanged interface like this one:

public class DateTimeWrapper : INotifyPropertyChanged
{
    private DateTime _dateTime;
    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged()
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs("Value"));
    }

    public DateTime Value
    {
        get { return _dateTime; }
        set
        {
            _dateTime = value;
            OnPropertyChanged();
        }
    }
}

and after that you can use a Bindig object like this:

var binding = new Binding("Value", _time, "Value", true);
binding.ControlUpdateMode = ControlUpdateMode.OnPropertyChanged;
dateTimePicker1.DataBindings.Add(binding);
演出会有结束 2024-09-06 07:24:35

我知道这有点旧,但我使用了这里找到的解决方案:

http://office.microsoft.com/en-us/infopath-help/display-the-date-and-time-in-separate-controls-HA010082330 .aspx

工作正常,是一个无代码解决方案

I know this is a bit old, but I used the solution found here:

http://office.microsoft.com/en-us/infopath-help/display-the-date-and-time-in-separate-controls-HA010082330.aspx

That works fine and is a no code solution

狠疯拽 2024-09-06 07:24:35

我的 WinForm 中有一个名为 EpDayOnly 的公共 DateTime 属性。
然后,我通过编写以下内容将 datetimepicker 的日期与该属性绑定:

myDateTimePicker.DataBindings.Add("Value", this, "EpDayOnly", false, DataSourceUpdateMode.OnPropertyChanged);

当我打开表单时,我通过编写来设置 datetime 属性

myNewForm.EpDayOnly=DateTime.Parse("5/5/2012");

,这会反映到 datetimepicker 控件。

I have a public DateTime property in my WinForm named EpDayOnly.
Then I bind the datetimepicker's date with the property by writing:

myDateTimePicker.DataBindings.Add("Value", this, "EpDayOnly", false, DataSourceUpdateMode.OnPropertyChanged);

When I open the form I set the datetime property by writing for example

myNewForm.EpDayOnly=DateTime.Parse("5/5/2012");

and this is reflected to the datetimepicker control.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文