[wpf]如何在后台写controltemplate.triggers
以下是我写的代码://CreateabuttonButtonbutton=newButton();button.Width=100;button.Height=100;b...
以下是我写的代码:
//Create a button
Button button = new Button();
button.Width = 100;
button.Height = 100;
button.Margin = new Thickness(10, 5, 0, 0);
button.Content = "Click Me!";
button.Click += TestButton_Click;
//Create a template
ControlTemplate template = new ControlTemplate(button.GetType());
FrameworkElementFactory canvas = new FrameworkElementFactory(typeof(Canvas));
template.VisualTree = canvas;
FrameworkElementFactory ellipse = new FrameworkElementFactory(typeof(Ellipse));
ellipse.SetValue(Ellipse.NameProperty, "templateEllipse");
ellipse.SetValue(Ellipse.WidthProperty, 100.0);
ellipse.SetValue(Ellipse.HeightProperty, 100.0);
ellipse.SetValue(Ellipse.FillProperty, new SolidColorBrush(Colors.Yellow));
canvas.AppendChild(ellipse);
FrameworkElementFactory label = new FrameworkElementFactory(typeof(Label));
label.SetValue(Label.MarginProperty, new Thickness(20, 35, 0, 0));
label.SetValue(Label.ContentProperty, new TemplateBindingExtension(Button.ContentProperty));
canvas.AppendChild(label);
//Create a trigger
Setter setter = new Setter();
setter.Property = Ellipse.FillProperty;
setter.Value = new SolidColorBrush(Colors.Green);
setter.TargetName = "templateEllipse";//此句运行的时候会出错!!
Trigger trigger = new Trigger();
trigger.Property = Button.IsMouseOverProperty;
trigger.Value = true;
trigger.Setters.Add(setter);
template.Triggers.Add(trigger);
//Add the template into the button
button.Template = template;
//Add the button into the canvas
xamlCanvas.Children.Add(button);
上面注释的地方有问题,但是无法解决,请高人赐教
非常感谢 展开
//Create a button
Button button = new Button();
button.Width = 100;
button.Height = 100;
button.Margin = new Thickness(10, 5, 0, 0);
button.Content = "Click Me!";
button.Click += TestButton_Click;
//Create a template
ControlTemplate template = new ControlTemplate(button.GetType());
FrameworkElementFactory canvas = new FrameworkElementFactory(typeof(Canvas));
template.VisualTree = canvas;
FrameworkElementFactory ellipse = new FrameworkElementFactory(typeof(Ellipse));
ellipse.SetValue(Ellipse.NameProperty, "templateEllipse");
ellipse.SetValue(Ellipse.WidthProperty, 100.0);
ellipse.SetValue(Ellipse.HeightProperty, 100.0);
ellipse.SetValue(Ellipse.FillProperty, new SolidColorBrush(Colors.Yellow));
canvas.AppendChild(ellipse);
FrameworkElementFactory label = new FrameworkElementFactory(typeof(Label));
label.SetValue(Label.MarginProperty, new Thickness(20, 35, 0, 0));
label.SetValue(Label.ContentProperty, new TemplateBindingExtension(Button.ContentProperty));
canvas.AppendChild(label);
//Create a trigger
Setter setter = new Setter();
setter.Property = Ellipse.FillProperty;
setter.Value = new SolidColorBrush(Colors.Green);
setter.TargetName = "templateEllipse";//此句运行的时候会出错!!
Trigger trigger = new Trigger();
trigger.Property = Button.IsMouseOverProperty;
trigger.Value = true;
trigger.Setters.Add(setter);
template.Triggers.Add(trigger);
//Add the template into the button
button.Template = template;
//Add the button into the canvas
xamlCanvas.Children.Add(button);
上面注释的地方有问题,但是无法解决,请高人赐教
非常感谢 展开
1个回答
展开全部
所提问题与错误无关.
你发生问题的地方在於 Ellipse.NameProperty, 对於作为 FrameworkElementFactory, 它的 Name 属性可不是通过设置 NameProperty来设定, 而必须是在构造时完成.
对於你的代码, 只要将两行稍作修改即可:
FrameworkElementFactory ellipse = new FrameworkElementFactory(typeof(Ellipse), "templateEllipse"); // 在构造中就指定名字
// ellipse.SetValue(Ellipse.NameProperty, "templateEllipse"); 将这行注释掉
你发生问题的地方在於 Ellipse.NameProperty, 对於作为 FrameworkElementFactory, 它的 Name 属性可不是通过设置 NameProperty来设定, 而必须是在构造时完成.
对於你的代码, 只要将两行稍作修改即可:
FrameworkElementFactory ellipse = new FrameworkElementFactory(typeof(Ellipse), "templateEllipse"); // 在构造中就指定名字
// ellipse.SetValue(Ellipse.NameProperty, "templateEllipse"); 将这行注释掉
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询