FrameworkElementFactory 动态添加Button
同通过如下代码动态添加了Button,但是点击事件点击没有生效,请问是什么原因呢Column item = new Column
{
Header = header,
IsReadOnly = true,
Binding = new Binding(path),
Width = new GridLength(whidth),
HorizontalAlignment = HorizontalAlignment.Center,
HeaderHorizontalAlignment = HorizontalAlignment.Center
};
if ("操作".Equals(header))
{
DataGridTemplateColumn dataGridTemplateColumn = new DataGridTemplateColumn();
dataGridTemplateColumn.Header = "操作";
dataGridTemplateColumn.Width = new DataGridLength(1, DataGridLengthUnitType.Star);
//Grid分列
FrameworkElementFactory gridFactory = new FrameworkElementFactory(typeof(Grid));
FrameworkElementFactory col1 = new FrameworkElementFactory(typeof(ColumnDefinition));
FrameworkElementFactory col2 = new FrameworkElementFactory(typeof(ColumnDefinition));
col1.SetValue(ColumnDefinition.WidthProperty, new GridLength(1, GridUnitType.Star));
col2.SetValue(ColumnDefinition.WidthProperty, new GridLength(1, GridUnitType.Star));
gridFactory.AppendChild(col1);
gridFactory.AppendChild(col2);
//添加两个Button
FrameworkElementFactory btn1Factory = new FrameworkElementFactory(typeof(Button));
btn1Factory.SetValue(Button.StyleProperty, Resources["ListEditButton"] as Style);
btn1Factory.SetValue(Button.MarginProperty, new Thickness(0, 0, 5, 0));
//添加事件
btn1Factory.AddHandler(Button.ClickEvent, new RoutedEventHandler(ModifyData_Click));
FrameworkElementFactory btn2Factory = new FrameworkElementFactory(typeof(Button));
btn2Factory.SetValue(Button.StyleProperty, Resources["ListDeleteButton"] as Style);
btn2Factory.AddHandler(Button.ClickEvent, new RoutedEventHandler(DeleteData_Click));
btn2Factory.SetValue(Grid.ColumnProperty, 1);
gridFactory.AppendChild(btn1Factory);
gridFactory.AppendChild(btn2Factory);
//关键代码
DataTemplate cellTemplate1 = new DataTemplate();
cellTemplate1.VisualTree = gridFactory;
Trigger trigger = new Trigger();
cellTemplate1.Seal();
dataGridTemplateColumn.CellTemplate = cellTemplate1;
item.CellTemplate = cellTemplate1;
}
return item;
你这个代码是wpf的吧,用的是哪个C1控件,麻烦上传一个能重现问题的demo,我帮你看一下原因 我用的是C1FlexGrid,我的需求是在每一行的列头添加两个Button,一个编辑一个删除,直接用 标签形式来布局是没有问题的,但是现在是需要动态生成c1FlexGrid 控件并生成Button,这样就点击不了,点击的时候进入不到对应的事件方法当中去 问题可以重现,需要调研一下原因,然后给你回复,这个帖子暂时保留处理
页:
[1]