首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未使用Frameworkelementfactory更新按钮内容

未使用Frameworkelementfactory更新按钮内容
EN

Stack Overflow用户
提问于 2012-02-15 23:13:24
回答 2查看 1.8K关注 0票数 1

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

代码可以工作,除了我不能设置按钮的内容。我尝试了很多东西,但我总是以一个带有Content=按钮的按钮而告终。

下面是生成contenttemplate的代码。为了获得更多信息,我在Tabcontrol Header Itemtemplate中使用了这一点...

干杯。

代码语言:javascript
复制
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;
EN

回答 2

Stack Overflow用户

发布于 2012-02-15 23:20:06

代码语言:javascript
复制
ControlTemplate ct = new ControlTemplate(typeof(TabItem));

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

(在我看来,其他一切都很好,而且the docs也不推荐使用FEF )

票数 0
EN

Stack Overflow用户

发布于 2017-01-19 23:48:01

对于那些仍在使用FEF的用户,我可以使用实际的字符串设置按钮的内容。我把Name看作是你例子中"button“的来源。在我的例子中,Name拉出了我绑定到我的datagrid的类名。

代码语言:javascript
复制
    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();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9295917

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档