DatePicker匿名事件处理程序

时间:2012-03-13 20:32:07

标签: c# wpf

我有一个以编程方式创建的WPF表单。它基本上是一个包含2个DatePicker个对象的项目列表,用于指定日期范围。我的一切都在工作,但我需要在SelectedDateChanged事件上激发一些逻辑。

问题是,因为DatePicker是根据前一个表单中的选择动态生成的,所以我需要使用匿名监听器(或者你在.NET中称之为的任何东西)。我一直无法按照我知道的方式去做,我似乎无法通过谷歌找到任何示例或帮助。提前感谢任何提示。

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

复选框有一个匿名事件处理程序,但这不适用于DatePicker.SelectedDateChanged事件。

1 个答案:

答案 0 :(得分:3)

你的意思是使用像这样的匿名方法的事件处理程序吗?

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