MonthCalendar.SelectionRange 数据绑定示例
Posted
技术标签:
【中文标题】MonthCalendar.SelectionRange 数据绑定示例【英文标题】:MonthCalendar.SelectionRange databinding example 【发布时间】:2008-12-18 08:04:36 【问题描述】:如何对 MonthCalendar.SelectionRange 属性执行数据绑定?鉴于该属性的类型为“SelectionRange”,这是一个类,我不知道该怎么做。任何示例将不胜感激。
【问题讨论】:
【参考方案1】:好吧,MonthCalendar
或 SelectionRange
上似乎没有任何明显的事件,也没有实现 INotifyPropertyChanged
,所以它看起来 像数据绑定在这里可能不行。
更新:但是,它确实会引发 DateChanged,因此您可以手动将一些东西连接在一起,或者(更有用)通过子类化控件来以适合绑定的方式公开值和事件。请注意Actual(...)
很有用,因为结束(否则)就在午夜之前,而不是午夜本身......
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows.Forms;
class Foo : INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
Debug.WriteLine(ToString());
private void SetField<T>(ref T field, T value, string propertyName)
if (!EqualityComparer<T>.Default.Equals(field, value))
field = value;
OnPropertyChanged(propertyName);
private DateTime start, end;
public DateTime Start get return start; set SetField(ref start, value, "Start");
public DateTime End get return end; set SetField(ref end, value, "End");
class BindableCalendar : MonthCalendar
public DateTime ActualSelectionStart
get return SelectionRange.Start;
set if (ActualSelectionStart != value) SetSelectionRange(value, ActualSelectionEnd);
public DateTime ActualSelectionEnd
get return SelectionRange.End;
set if (ActualSelectionEnd != value) SetSelectionRange(ActualSelectionStart, value);
// should really use EventHandlerList here...
public event EventHandler ActualSelectionStartChanged, ActualSelectionEndChanged;
DateTime lastKnownStart, lastKnownEnd;
protected override void OnDateChanged(DateRangeEventArgs drevent)
base.OnDateChanged(drevent);
if (lastKnownStart != drevent.Start)
if (ActualSelectionStartChanged != null) ActualSelectionStartChanged(this, EventArgs.Empty);
lastKnownStart = drevent.Start;
if (lastKnownEnd != drevent.End)
if (ActualSelectionEndChanged != null) ActualSelectionEndChanged(this, EventArgs.Empty);
lastKnownEnd = drevent.End;
static class Program
[STAThread]
static void Main()
Application.EnableVisualStyles();
MonthCalendar cal;
Button btn;
using (Form form = new Form
Controls =
(cal = new BindableCalendar Dock = DockStyle.Fill, MaxSelectionCount = 10 ),
(btn = new Button Dock = DockStyle.Bottom, Text = "thwack")
)
Foo foo = new Foo Start = DateTime.Today, End = DateTime.Today.AddDays(1) ;
cal.DataBindings.Add("ActualSelectionStart", foo, "Start").DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged;
cal.DataBindings.Add("ActualSelectionEnd", foo, "End").DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged;
btn.Click += delegate
foo.Start = foo.Start.AddDays(1);
foo.End = foo.End.AddDays(1);
;
Application.Run(form);
【讨论】:
【参考方案2】:对我来说,这似乎很简单。我只是绑定了 MonthCalendar 组件的 SelectionStart 和 SelectionEnd 属性。
this.Calendar1.DataBindings.Add(new System.Windows.Forms.Binding("SelectionStart",
bindingSource, "DateField", true));
this.Calendar1.DataBindings.Add(new System.Windows.Forms.Binding("SelectionEnd",
bindingSource, "DateField", true));
【讨论】:
以上是关于MonthCalendar.SelectionRange 数据绑定示例的主要内容,如果未能解决你的问题,请参考以下文章