Matt Grogan
Matt Grogan

Reputation: 317

DatePicker anonymous event handler

I have a WPF form that I have created programatically. It is basically a list of items with 2 DatePicker objects to specify a range of dates. I have everything working for the most part, but I need to fire some logic on the SelectedDateChanged event.

The problem is, since the DatePicker's are generated dynamically based on selections from a previous form, I need to use an anonymous listener (or whatever you call them in .NET). I have been unable to do it the way I know how and I cant seem to find any example or help through google either. Thanks in advance for any tips.

Generating DatePickers:

public SystemInterval(Role role)
    {
        this.role = role;
        InitializeComponent();
        lbls = new Label[role.RoleSystems.Length];
        dp = new DatePicker[role.RoleSystems.Length * 2];
        cks = new CheckBox[role.RoleSystems.Length];
        ScrollViewer sv = new ScrollViewer();
        sv.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
        sv.VerticalAlignment = System.Windows.VerticalAlignment.Top;
        sv.Margin = new Thickness(0,50,0,40);
        Grid g = new Grid();
        g.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
        g.VerticalAlignment = System.Windows.VerticalAlignment.Top;
        ColumnDefinition col1 = new ColumnDefinition();
        col1.Width = new GridLength(46);
        ColumnDefinition col2 = new ColumnDefinition();
        col2.Width = GridLength.Auto;
        ColumnDefinition col3 = new ColumnDefinition();
        col3.Width = new GridLength(150);
        ColumnDefinition col4 = new ColumnDefinition();
        col4.Width = new GridLength(150);
        g.ColumnDefinitions.Add(col1);
        g.ColumnDefinitions.Add(col2);
        g.ColumnDefinitions.Add(col3);
        g.ColumnDefinitions.Add(col4);
        sv.Content = g;
        LayoutRoot.Children.Add(sv);

        for (int r = 0; r < cks.Length; r++)
        {
            g.RowDefinitions.Add(new RowDefinition());
        }

        g.Height = (lbls.Length * 25) + (lbls.Length * 5);

        for (int i = 0, j = 0; i < cks.Length; i++, j+=2)
        {
            cks[i] = new CheckBox();
            cks[i].IsChecked = false;
            cks[i].VerticalAlignment = System.Windows.VerticalAlignment.Center;
            cks[i].HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
            Grid.SetColumn(cks[i], 0);
            Grid.SetRow(cks[i], i);
            g.Children.Add(cks[i]);

            lbls[i] = new Label();
            lbls[i].Height = 25;
            lbls[i].Content = role.RoleSystems[i].SystemName;
            lbls[i].VerticalAlignment = System.Windows.VerticalAlignment.Center;
            lbls[i].HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
            Grid.SetColumn(lbls[i], 1);
            Grid.SetRow(lbls[i], i);
            g.Children.Add(lbls[i]);

            dp[j] = new DatePicker();
            dp[j].Height = 25;
            dp[j].Width = 115;
            dp[j].VerticalAlignment = System.Windows.VerticalAlignment.Center;
            dp[j].HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
            dp[j].IsEnabled = false;
            dp[j].BlackoutDates.Add(new CalendarDateRange(new DateTime(0001, 1, 1), DateTime.Now.Subtract(TimeSpan.FromDays(1))));
            Grid.SetColumn(dp[j], 2);
            Grid.SetRow(dp[j], i);
            g.Children.Add(dp[j]);

            dp[j + 1] = new DatePicker();
            dp[j + 1].Height = 25;
            dp[j + 1].Width = 115;
            dp[j + 1].VerticalAlignment = System.Windows.VerticalAlignment.Center;
            dp[j + 1].HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
            dp[j + 1].IsEnabled = false;
            dp[j + 1].BlackoutDates.Add(new CalendarDateRange(new DateTime(0001, 1, 1), DateTime.Now));
            Grid.SetColumn(dp[j + 1], 3);
            Grid.SetRow(dp[j + 1], i);
            g.Children.Add(dp[j + 1]);

            cks[i].Click += new RoutedEventHandler(delegate(object s, RoutedEventArgs re)
            {
                CheckBox cb = (CheckBox)re.Source;
                for (int r = 0; r < cks.Length; r++)
                {
                    if (cks[r].Equals(cb))
                    {
                        dp[r * 2].IsEnabled = true;
                        dp[r * 2 + 1].IsEnabled = true;
                    }
                }
            });
        }

The checkboxs have an anonymous event handler, however this does not work for the DatePicker.SelectedDateChanged event.

Upvotes: 0

Views: 686

Answers (1)

Phil
Phil

Reputation: 42991

Do you mean an event handler using an anonymous method like this?

var dp = new DatePicker();
dp.SelectedDateChanged +=
    (sender, args) =>
        {
            var picker = (DatePicker) sender;
            /* do stuff here */
        };

Upvotes: 3

Related Questions