2012-02-15 54 views
1

我一直在尝试使用可爱的Frameworkelementfactory创建ContentTemplate。使用Frameworkelementfactory不更新按钮内容

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

下面是生成contenttemplate的代码。为了您的更多信息,我正在使用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; 

回答

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

你不是应该在这里创建DataTemplate呢?

(其他的一切看起来好像没什么问题,也FEF已过时,读the docs

+0

您好,我已阅读文档。它是一个TabItem,我设置了TabItem.Template,虽然在XAML中是Datatemplate类型,但我无法将其转换为Datatemplate。使用Controltemplate排序的作品!使用Xaml.load非常慢,也不能投射。是的,我已阅读它已被弃用。但为什么应该处理程序工作,但不是内容? – stackeroverflow 2012-02-15 16:52:14

0

对于那些使用仍然使用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();