//设置选中菜品的效果 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 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 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; } } } }