最近项目有个需求需要动态扩展DataGrid,动态扩展列,cell的模板不一样,有的textbox,有的combox,并且需要后台绑定style设置datatrigger
先看combox的
DataGridTemplateColumn col = new DataGridTemplateColumn();
var stackPanelFactory = new FrameworkElementFactory(typeof(StackPanel));
stackPanelFactory.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);
var comboBox = new FrameworkElementFactory(typeof(ComboBox));
comboBox.SetValue(ComboBox.ItemsSourceProperty, item.recipeParameter.DiscriptionList);
comboBox.SetValue(ComboBox.SelectedIndexProperty, 0);
comboBox.SetValue(ComboBox.TextProperty, item.recipeParameter.DiscriptionList[Convert.ToInt32(item.recipeParameter.DefaultValue)]);
comboBox.AddHandler(ComboBox.GotFocusEvent, new RoutedEventHandler(DGCombox_GotFocus));
//comboBox.SetBinding(ComboBox.TextProperty, new Binding(item.ColumeName));
Style style = new Style();
style.TargetType = typeof(ComboBox);
DataTrigger dataTrigger = new DataTrigger();
dataTrigger.Binding = new Binding
{
Path = new PropertyPath("SelectedIndex"),
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
Mode = BindingMode.Default,
RelativeSource = RelativeSource.Self,
};
dataTrigger.Value = "-1";
dataTrigger.Setters.Add(new Setter(FrameworkElement.VisibilityProperty, Visibility.Hidden));
dataTrigger.Setters.Add(new Setter(FrameworkElement.IsEnabledProperty, false));
style.Triggers.Add(dataTrigger);
comboBox.SetValue(StyleProperty, style);
comboBox.SetBinding(ComboBox.TextProperty,
new Binding()
{
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
Path = new PropertyPath(item.ColumeName),
});
stackPanelFactory.AppendChild(comboBox);
var dataTemplate = new DataTemplate
{
VisualTree = stackPanelFactory
};
textbox的
var stackPanelFactory = new FrameworkElementFactory(typeof(StackPanel));
stackPanelFactory.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);
var textBox = new FrameworkElementFactory(typeof(TextBox));
textBox.SetBinding(TextBox.TextProperty, new Binding(item.ColumeName));
Style style = new Style();
style.TargetType = typeof(TextBox);
DataTrigger dataTrigger = new DataTrigger();
dataTrigger.Binding = new Binding
{
Path = new PropertyPath("Text"),
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
Mode = BindingMode.TwoWay,
RelativeSource = RelativeSource.Self,
};
dataTrigger.Value = "'";
dataTrigger.Setters.Add(new Setter(FrameworkElement.VisibilityProperty, Visibility.Hidden));
style.Triggers.Add(dataTrigger);
textBox.SetValue( StyleProperty, style);
textBox.SetBinding(TextBox.TextProperty,
new Binding()
{
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
Path = new PropertyPath(item.ColumeName),
}
);
if (item.ColumeName != "Parameter" && item.ColumeName != "Step Name")
{
if (item.recipeParameter.ParamType == "int" || item.recipeParameter.ParamType == "long" || item.recipeParameter.ParamType == "short")
{
textBox.SetValue(ToolTipProperty, item.recipeParameter.Min + "-" + item.recipeParameter.Max);
textBox.SetValue(TagProperty, item.recipeParameter.DefaultValue);
textBox.AddHandler(TextBox.PreviewTextInputEvent, new TextCompositionEventHandler(NumberText_PreviewTextInput), true);
textBox.AddHandler(TextBox.TextChangedEvent, new TextChangedEventHandler(TextBox_TextChanged), true);
textBox.AddHandler(TextBox.LostFocusEvent, new RoutedEventHandler(TextBox_LostFocus));
}
else if (item.recipeParameter.ParamType == "double")
{
textBox.SetValue(ToolTipProperty, item.recipeParameter.Min + "-" + item.recipeParameter.Max);
textBox.SetValue(TagProperty, item.recipeParameter.DefaultValue);
textBox.AddHandler(TextBox.PreviewTextInputEvent, new TextCompositionEventHandler(NumberdoubleText_PreviewTextInput), true);
textBox.AddHandler(TextBox.TextChangedEvent, new TextChangedEventHandler(TextBox_TextChanged), true);
textBox.AddHandler(TextBox.LostFocusEvent, new RoutedEventHandler(TextBox_LostFocus));
}
}
textBox.SetValue(BorderBrushProperty, null);
textBox.SetValue(MinWidthProperty, (double)100);
stackPanelFactory.AppendChild(textBox);
var dataTemplate = new DataTemplate
{
VisualTree = stackPanelFactory
};
var templateColumn = new DataGridTemplateColumn
{
Header = item.ColumeName,
CellTemplate = dataTemplate
};