public StackPanel GetFoodsStackPanel(List <Food> foods) { StackPanel st = new StackPanel(); foreach (Food food in foods) { Label lb = new Label(); lb.Content = food.name; st.Children.Add(lb); lb.AddHandler(MouseDownEvent, new RoutedEventHandler((object sender, RoutedEventArgs e) => { MessageBox.Show(food.contents.Replace('|', '\n')); })); } st.Width = 200; return(st); }
public override void ini(ref int row, ref int col) { //注册到主面板中 subgrid.Margin = new Thickness(1, 2, 1, 2); add_ctrl(subgrid, ref row, ref col); //加入鼠标事件 tb.AddHandler(UIElement.MouseDownEvent, new RoutedEventHandler(mouseDown), true); lb_on.AddHandler(UIElement.MouseDownEvent, new RoutedEventHandler(mouseDown), true); lb_off.AddHandler(UIElement.MouseDownEvent, new RoutedEventHandler(mouseDown), true); //控件自身的属性 tb.Content = cmddes.name; tb.Tag = cmddes.name; FrameworkElementFactory f = new FrameworkElementFactory(typeof(Border), "Border"); f.SetValue(Border.CornerRadiusProperty, new CornerRadius(15)); f.SetValue(Border.BackgroundProperty, br_normal); FrameworkElementFactory f1 = new FrameworkElementFactory(typeof(ContentPresenter), "ContentPresenter"); Binding bindingc = new Binding("Content"); bindingc.Source = tb; bindingc.Mode = BindingMode.OneWay; f1.SetBinding(ContentPresenter.ContentProperty, bindingc); Binding bindingb = new Binding("Background"); bindingb.Source = tb; bindingb.Mode = BindingMode.OneWay; f.SetBinding(Border.BackgroundProperty, bindingb); f1.SetValue(FrameworkElement.VerticalAlignmentProperty, VerticalAlignment.Center); f1.SetValue(FrameworkElement.HorizontalAlignmentProperty, HorizontalAlignment.Center); f.AppendChild(f1); ControlTemplate ct = new ControlTemplate(typeof(Button)); ct.VisualTree = f; tb.Template = ct; lb_on.Content = "开"; lb_off.Content = "关"; add_to_grid(bd, 0); Grid.SetColumnSpan(bd, 3); bd.BorderBrush = Brushes.LightGray; bd.Background = new SolidColorBrush(Color.FromRgb(0xbb, 0xbb, 0xbb)); bd.CornerRadius = new CornerRadius(15); bd.BorderThickness = new Thickness(3); add_to_grid(lb_on, 0); add_to_grid(lb_off, 2); add_to_grid(tb, 1); lb_off.HorizontalAlignment = HorizontalAlignment.Right; sw_action(8); //控件的接收配置 if (commc.dset.ContainsKey(cmddes.refdname)) //若有反馈值 { var t = commc.dset[cmddes.refdname]; judge_out(cmddes.dft); //t.update_cb = tn => { t.update_times = 10; };//数据更新回调函数 t.update_dis += delegate(string tn) //数据更新回调函数 { if (t.update_times > 0) { //t.update_times--; tb.Background = Brushes.LightYellow; if (Mouse.LeftButton == MouseButtonState.Pressed) { return; //鼠标按下就先不刷 } judge_out(t.val); } else { tb.Background = br_normal; } }; } }