문제

MonthCalendar.SelectionRange 속성에 대한 데이터 바인딩을 어떻게 수행합니까? 속성이 'selectionRange'유형이라는 점을 감안할 때 클래스 인 클래스 인 저는 어떻게 해야할지 잘 모르겠습니다. 모든 예제는 대단히 감사 할 것입니다.

도움이 되었습니까?

해결책

글쎄, 이것에 대한 명백한 사건이없는 것 같습니다. 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);
        }
    }
}

다른 팁

나에게는 매우 간단한 것 같습니다. 나는 MonthCalendar 구성 요소의 selectionStart 및 선택 엔드 속성을 바인딩합니다.

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));
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top