代码之家  ›  专栏  ›  技术社区  ›  Thomas Stock

在WPF中以编程方式创建ControlTemplate

  •  5
  • Thomas Stock  · 技术社区  · 16 年前

    Polygon buttonPolygon = new Polygon();
    buttonPolygon.Points = buttonPointCollection;
    buttonPolygon.Stroke = Brushes.Yellow;
    buttonPolygon.StrokeThickness = 2;
    
    // create ControlTemplate based on polygon
    ControlTemplate template = new ControlTemplate();
    template.Childeren.Add(buttonPolygon); // This does not work! What's the right way?
    
    //create button based on controltemplate
    Button button = new Button();
    button.Template = template;
    

    所以我需要一种方法来设置我的多边形作为按钮的模板。建议?

    谢谢。

    1 回复  |  直到 14 年前
        1
  •  6
  •   Community Mohan Dere    9 年前

    更结构化的方法是使用FrameworkElementFactory类--创建一个FrameworkElementFactory并将ControlTemplate.VisualTree设置为该FEF。这提高了类型安全性,并避免了编写对象树只是为了再次读入而产生的麻烦。但是,它已经被官方弃用了,如果你有一个复杂的模板,它会变得相当复杂。

    看到了吗 How to setup a WPF datatemplate in code for a treeview?

    推荐文章