按钮内容未使用Frameworkelementfactory更新

本文关键字:Frameworkelementfactory 更新 未使用 按钮 | 更新日期: 2023-09-27 18:24:37

我一直在尝试使用可爱的Frameworkelementfactory创建一个ContentTemplate。

除了我不能设置按钮的内容外,代码还能工作。我尝试过很多东西,但最终总是得到一个Content=Button的按钮。

这是生成内容模板的代码。对于您的进一步信息,我正在Tabcontrol Header Itemtemplate中使用此。。。

干杯。

ControlTemplate ct = new ControlTemplate(typeof(TabItem));
FrameworkElementFactory spouter = new FrameworkElementFactory(typeof    (DockPanel));
FrameworkElementFactory text = new FrameworkElementFactory(typeof(TextBlock));
text.SetValue(TextBlock.TextProperty, Name);
spouter.AppendChild(text);
FrameworkElementFactory mButtonPrev = new FrameworkElementFactory(typeof(Button));
mButtonPrev.SetValue(System.Windows.Controls.Button.ContentProperty, "x");
mButtonPrev.AddHandler(System.Windows.Controls.Button.ClickEvent, new RoutedEventHandler(CloseTab));
spouter.AppendChild(mButtonPrev);
ct.VisualTree = spouter;
return ct;

按钮内容未使用Frameworkelementfactory更新

ControlTemplate ct = new ControlTemplate(typeof(TabItem));

你不应该在这里创建一个DataTemplate吗?

(其他一切对我来说都很好,FEF也不推荐使用,请阅读文档)

对于那些仍在使用FEF的用户,我可以用实际字符串设置按钮的内容。在您的示例中,我将Name视为"button"的来源。在我的示例中,Name提取了我绑定到数据网格的类名。

    var buttonTemplate = new FrameworkElementFactory(typeof(Button));
    var text = new FrameworkElementFactory(typeof(TextBlock));
    text.SetValue(TextBlock.TextProperty, "Save");
    buttonTemplate.AppendChild(text);
    buttonTemplate.AddHandler(
        System.Windows.Controls.Primitives.ButtonBase.ClickEvent,
        new RoutedEventHandler((o, e) => MessageBox.Show("hi"))
    );
    AccountDatagrid.Columns.Add(new DataGridTemplateColumn()
    {
        Header = "Save",
        CellTemplate = new DataTemplate() { VisualTree = buttonTemplate }
    });
    AccountDatagrid.ItemsSource = AccoutDescCodeTime.GetBaseAccounts();
相关文章:
  • 没有找到相关文章