//设置选中菜品的效果 private void SetSelectedFood() { var items = this.FoodsDiv.ItemContainerGenerator.Items; if (items != null) { for (int i = 0; i < items.Count; i++) { ListBoxItem boxItem = this.FoodsDiv.ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem; Button b = TreeHelpUtils.FindVisualTree <Button>(boxItem); if (b == null) { break; } if (!foodList.isSelected) //全消除 { b.Background = new SolidColorBrush(Color.FromArgb(255, 221, 221, 221)); } else { Food f = (Food)items[i]; if (f.Id == foodList.lastSelectedFoodId) //取消过去的 { b.Background = new SolidColorBrush(Color.FromArgb(255, 221, 221, 221)); } else if (f.Id == foodList.selectedFoodId) { b.Background = Brushes.Orange; } } } } }
//菜单选中 private void MenuFood_MouseDown(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()); //MessageBox.Show("点击者" + e.Source.GetType()); Grid grid = e.OriginalSource as Grid; //获取事件发起源头 ContentPresenter cp = grid.TemplatedParent as ContentPresenter; //获取模板目标 //List<TextBox> tbs = SearchVisualTree<TextBox>(cp); TextBox tb = TreeHelpUtils.FindVisualTree <TextBox>(cp); int index = int.Parse(tb.Text) - 1; Console.Out.WriteLine(this.FoodMenuDiv.Items[index].GetType()); Console.Out.WriteLine((this.FoodMenuDiv.Items[index] as Food).Id); SetMessageWin("选中序号:" + index + "\n 菜品名:" + (this.FoodMenuDiv.Items[index] as Food).Name); if (index < 0) { MessageBox.Show("序号错误!"); } //foodMenu.MenuSelected(index); SetSelectedMenu(foodList.currentMenuSelectedIndex, index); foodList.MenuSelected(index); //SetSelectedMenu(index, true); Console.Out.WriteLine(index); }
//菜品双击事件 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 SetSelectedMenu(int lass_index, int current_index) { if (lass_index == current_index) { if (lass_index == -1) { return; } else { ListBoxItem boxItem = this.FoodMenuDiv.ItemContainerGenerator.ContainerFromIndex(lass_index) as ListBoxItem; //ok能找到 //List<TextBox> tbs = TreeHelpUtils.FindListVisualTree<TextBox>(boxItem); Grid g = TreeHelpUtils.FindVisualTree <Grid>(boxItem); if (g.Background == null) { g.Background = Brushes.Orange; } else { g.Background = null; } } } else { if (lass_index != -1) { ListBoxItem boxItem = this.FoodMenuDiv.ItemContainerGenerator.ContainerFromIndex(lass_index) as ListBoxItem; //ok能找到 //List<TextBox> tbs = TreeHelpUtils.FindListVisualTree<TextBox>(boxItem); Grid g = TreeHelpUtils.FindVisualTree <Grid>(boxItem); g.Background = null; } if (current_index != -1) { ListBoxItem boxItem = this.FoodMenuDiv.ItemContainerGenerator.ContainerFromIndex(current_index) as ListBoxItem; //ok能找到 //List<TextBox> tbs = TreeHelpUtils.FindListVisualTree<TextBox>(boxItem); Grid g = TreeHelpUtils.FindVisualTree <Grid>(boxItem); g.Background = Brushes.Orange; } } //TreeHelpUtils.FindVisualTree<Grid>(boxItem); }
//菜品单击 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); }
//设置选中菜品类的效果 private void SetSelectedFoodClass() { var items = this.FoodClassDiv.ItemContainerGenerator.Items; if (items != null) { for (int i = 0; i < items.Count; i++) { ListBoxItem boxItem = this.FoodClassDiv.ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem; Button b = TreeHelpUtils.FindVisualTree <Button>(boxItem); if (b == null) { break; } //全消除 b.Background = new SolidColorBrush(Color.FromArgb(255, 221, 221, 221)); FoodClass f = (FoodClass)items[i]; if (f == foodList.CurrentFoodClass) { b.Background = Brushes.Orange; } } } }