在XAML中为一个事件添加多个事件处理程序?

时间:2010-08-01 20:46:51

标签: c# wpf events xaml

程序代码中的

可以执行以下操作:

// Add two event handler for the button click event
button1.Click += new RoutedEventHandler(button1_Click_1);
button1.Click += new RoutedEventHandler(button1_Click_2);

但是如何在XAML中为按钮的点击事件添加多个事件处理程序? 谢谢你的提示!

2 个答案:

答案 0 :(得分:8)

您无法在XAML中订阅多个事件处理程序。但是,您可以通过订阅单个事件处理程序然后从事件处理程序调用两个或更多方法来实现相同的效果。

    private void Button_OnClick(object sender, RoutedEventArgs e)
    {
        ButtonOnClick1();
        ButtonOnClick2();
    }

    private void ButtonOnClick1()
    {
        //Do something...
    }

    private void ButtonOnClick2()
    {
        //Do something...
    }

答案 1 :(得分:1)

您可以在protected $dates = ['date_from', 'date_to'];中指定多个处理程序,如下所示:

$min_date = Reservation::select(DB::raw('MIN(date_from) as min'))->first();
$max_date = Reservation::select(DB::raw('MIN(date_to) as max'))->first();
$data = [];

if (!is_null($min_date) && !is_null($max_date)) {
    $daterange = new DatePeriod(DateTime::createFromFormat('Y-m-d H:i:s', $min_date->min), new DateInterval('P1D') ,DateTime::createFromFormat('Y-m-d H:i:s', $max_date->max));

    $reservations = Reservation::all();
    foreach ($daterange as $date) {
        $data[$date->format('Y-m-d')] = $reservations->filter(function($reservation) use($date) {
            $carbon_date = Carbon::instance($date)->endOfDay();
            return ($reservation->date_from->startOfDay()->lte($carbon_date) && $reservation->date_to->endOfDay()->gte($carbon_date));
        })
        ->count();
    }
}