//菜品双击事件 private void Button_MouseDoubleClick(object sender, MouseButtonEventArgs e) { Console.Out.WriteLine("sender.GetType() = " + sender.GetType()); Console.Out.WriteLine("e.Source.GetType() = " + e.Source.GetType()); Console.Out.WriteLine("e.OriginalSource.GetType() = " + e.OriginalSource.GetType()); Button bt = (e.Source as Button); //ContentPresenter cp = bt.TemplatedParent as ContentPresenter; //获取模板目标 List <Label> lbs = TreeHelpUtils.FindListVisualTree <Label>(bt); string id = lbs[0].Content.ToString(); //返回lbs[4] 0 id 1 name 2 price 3 ¥ Console.Out.WriteLine("双击! id = " + id); Food food = foodList.GetFoodById(id, foodList.CurrentFoodClass.FClass); if (food != null) { //如果是single模式 直接加1 否则读取输入框添加 if (food.St == "single") { food.SetFoodNum(100); AddFoodToMenu(food); } else { string s_number = this.NumberText.Text; double number = 0; if (double.TryParse(s_number, out number)) { if (food.SetFoodNum((int)(number * 100))) { AddFoodToMenu(food); } else { MessageBox.Show("添加失败!可能是数据输入与菜品类型不一致!"); } } else { MessageBox.Show("数据输入有误!"); } } } //双击结束取消菜品选中 foodList.SelectFood(""); SetSelectedFood(); }
//菜品单击 private void Button_FoodClick(object sender, RoutedEventArgs e) { Console.Out.WriteLine("sender.GetType() = " + sender.GetType()); Console.Out.WriteLine("e.Source.GetType() = " + e.Source.GetType()); Console.Out.WriteLine("e.OriginalSource.GetType() = " + e.OriginalSource.GetType()); Button bt = (e.Source as Button); ContentPresenter cp = bt.TemplatedParent as ContentPresenter; //获取模板目标 List <Label> lbs = TreeHelpUtils.FindListVisualTree <Label>(cp); //返回lbs[4] 0 id 1 name 2 price 3 ¥ string foodid = lbs[0].Content.ToString(); //下面这两条的顺序不能换! 不然select会改变之前的foodid foodList.SelectFood(foodid); SetSelectedFood(); //设置消息 Food food = foodList.GetFoodById(foodid, foodList.CurrentFoodClass.FClass); SetMessageWin("选中id:" + foodid + "\n类型:" + food.St); Console.Out.WriteLine("单击! id = " + foodid); }