【问题标题】:Button content not updating using Frameworkelementfactory未使用 Frameworkelementfactory 更新按钮内容
【发布时间】:2012-02-15 15:13:23
【问题描述】:

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

除了我无法设置 Button 的内容外,该代码有效。我尝试了很多东西,但我总是得到一个 Content= Button 的 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;

【问题讨论】:

    标签: c# wpf dynamic


    【解决方案1】:
    ControlTemplate ct = new ControlTemplate(typeof(TabItem));
    

    您不应该在这里创建DataTemplate 吗?

    (在我看来,其他一切都很好,FEF 也已弃用,请阅读the docs

    【讨论】:

    • 您好,已阅读文档。它是一个 TabItem,我正在设置 TabItem.Template,虽然在 XAML 中是 Datatemplate 类型,但我无法转换为 Datatemplate。使用 Controltemplate 之类的作品!使用 Xaml.load 真的很慢,也不能强制转换。是的,我已经读过它已被弃用。但是为什么处理程序应该工作而不是内容呢?
    【解决方案2】:

    对于仍在使用 FEF 的用户,我可以使用实际字符串设置按钮的内容。在您的示例中,我将 Name 视为“按钮”的来源。在我的示例中,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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-02
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-22
      相关资源
      最近更新 更多