当前位置:   article > 正文

WPF 后台代码动态向DataGrid添加TextBox、Combox并设置datatrigger

WPF 后台代码动态向DataGrid添加TextBox、Combox并设置datatrigger

最近项目有个需求需要动态扩展DataGrid,动态扩展列,cell的模板不一样,有的textbox,有的combox,并且需要后台绑定style设置datatrigger

先看combox的

  1. DataGridTemplateColumn col = new DataGridTemplateColumn();
  2. var stackPanelFactory = new FrameworkElementFactory(typeof(StackPanel));
  3. stackPanelFactory.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);
  4. var comboBox = new FrameworkElementFactory(typeof(ComboBox));
  5. comboBox.SetValue(ComboBox.ItemsSourceProperty, item.recipeParameter.DiscriptionList);
  6. comboBox.SetValue(ComboBox.SelectedIndexProperty, 0);
  7. comboBox.SetValue(ComboBox.TextProperty, item.recipeParameter.DiscriptionList[Convert.ToInt32(item.recipeParameter.DefaultValue)]);
  8. comboBox.AddHandler(ComboBox.GotFocusEvent, new RoutedEventHandler(DGCombox_GotFocus));
  9. //comboBox.SetBinding(ComboBox.TextProperty, new Binding(item.ColumeName));
  10. Style style = new Style();
  11. style.TargetType = typeof(ComboBox);
  12. DataTrigger dataTrigger = new DataTrigger();
  13. dataTrigger.Binding = new Binding
  14. {
  15. Path = new PropertyPath("SelectedIndex"),
  16. UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
  17. Mode = BindingMode.Default,
  18. RelativeSource = RelativeSource.Self,
  19. };
  20. dataTrigger.Value = "-1";
  21. dataTrigger.Setters.Add(new Setter(FrameworkElement.VisibilityProperty, Visibility.Hidden));
  22. dataTrigger.Setters.Add(new Setter(FrameworkElement.IsEnabledProperty, false));
  23. style.Triggers.Add(dataTrigger);
  24. comboBox.SetValue(StyleProperty, style);
  25. comboBox.SetBinding(ComboBox.TextProperty,
  26. new Binding()
  27. {
  28. UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
  29. Path = new PropertyPath(item.ColumeName),
  30. });
  31. stackPanelFactory.AppendChild(comboBox);
  32. var dataTemplate = new DataTemplate
  33. {
  34. VisualTree = stackPanelFactory
  35. };

textbox的

  1. var stackPanelFactory = new FrameworkElementFactory(typeof(StackPanel));
  2. stackPanelFactory.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);
  3. var textBox = new FrameworkElementFactory(typeof(TextBox));
  4. textBox.SetBinding(TextBox.TextProperty, new Binding(item.ColumeName));
  5. Style style = new Style();
  6. style.TargetType = typeof(TextBox);
  7. DataTrigger dataTrigger = new DataTrigger();
  8. dataTrigger.Binding = new Binding
  9. {
  10. Path = new PropertyPath("Text"),
  11. UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
  12. Mode = BindingMode.TwoWay,
  13. RelativeSource = RelativeSource.Self,
  14. };
  15. dataTrigger.Value = "'";
  16. dataTrigger.Setters.Add(new Setter(FrameworkElement.VisibilityProperty, Visibility.Hidden));
  17. style.Triggers.Add(dataTrigger);
  18. textBox.SetValue( StyleProperty, style);
  19. textBox.SetBinding(TextBox.TextProperty,
  20. new Binding()
  21. {
  22. UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
  23. Path = new PropertyPath(item.ColumeName),
  24. }
  25. );
  26. if (item.ColumeName != "Parameter" && item.ColumeName != "Step Name")
  27. {
  28. if (item.recipeParameter.ParamType == "int" || item.recipeParameter.ParamType == "long" || item.recipeParameter.ParamType == "short")
  29. {
  30. textBox.SetValue(ToolTipProperty, item.recipeParameter.Min + "-" + item.recipeParameter.Max);
  31. textBox.SetValue(TagProperty, item.recipeParameter.DefaultValue);
  32. textBox.AddHandler(TextBox.PreviewTextInputEvent, new TextCompositionEventHandler(NumberText_PreviewTextInput), true);
  33. textBox.AddHandler(TextBox.TextChangedEvent, new TextChangedEventHandler(TextBox_TextChanged), true);
  34. textBox.AddHandler(TextBox.LostFocusEvent, new RoutedEventHandler(TextBox_LostFocus));
  35. }
  36. else if (item.recipeParameter.ParamType == "double")
  37. {
  38. textBox.SetValue(ToolTipProperty, item.recipeParameter.Min + "-" + item.recipeParameter.Max);
  39. textBox.SetValue(TagProperty, item.recipeParameter.DefaultValue);
  40. textBox.AddHandler(TextBox.PreviewTextInputEvent, new TextCompositionEventHandler(NumberdoubleText_PreviewTextInput), true);
  41. textBox.AddHandler(TextBox.TextChangedEvent, new TextChangedEventHandler(TextBox_TextChanged), true);
  42. textBox.AddHandler(TextBox.LostFocusEvent, new RoutedEventHandler(TextBox_LostFocus));
  43. }
  44. }
  45. textBox.SetValue(BorderBrushProperty, null);
  46. textBox.SetValue(MinWidthProperty, (double)100);
  47. stackPanelFactory.AppendChild(textBox);
  48. var dataTemplate = new DataTemplate
  49. {
  50. VisualTree = stackPanelFactory
  51. };
  52. var templateColumn = new DataGridTemplateColumn
  53. {
  54. Header = item.ColumeName,
  55. CellTemplate = dataTemplate
  56. };

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/582841
推荐阅读
相关标签
  

闽ICP备14008679号