Pregunta

¿Cómo se realiza el enlace de datos contra la propiedad MonthCalendar.SelectionRange? Dada la propiedad es de 'SelectionRange' tipo que es una clase que no estoy seguro de cómo hacerlo. Cualquier ejemplo sería muy apreciada.

¿Fue útil?

Solución

Bueno, no parece que haya ningún evento obvias para esto ya sea en el MonthCalendar o la SelectionRange, y tampoco implementa INotifyPropertyChanged, por lo que ve como enlace de datos podría no ser posible aquí.

Actualización: Lo hace, sin embargo, elevar el dateChanged, por lo que podría enganchar algunas cosas juntos manualmente, o (más útil) como subclase el control para exponer los valores y los acontecimientos de una manera adecuada para la unión. Tenga en cuenta el Actual(...) son útiles porque el final (lo contrario) es justo antes de medianoche, en lugar de la medianoche en sí ...

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

Otros consejos

Para mí parece ser muy simple. Acabo obligado propiedades selectionStart y SelectionEnd del componente MonthCalendar.

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));
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top