我一直在尝试使用可爱的Frameworkelementfactory创建一个ContentTemplate。
代码可以工作,除了我不能设置按钮的内容。我尝试了很多东西,但我总是以一个带有Content=按钮的按钮而告终。
下面是生成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;发布于 2012-02-15 23:20:06
ControlTemplate ct = new ControlTemplate(typeof(TabItem));你不应该在这里创建一个DataTemplate吗?
(在我看来,其他一切都很好,而且the docs也不推荐使用FEF )
发布于 2017-01-19 23:48:01
对于那些仍在使用FEF的用户,我可以使用实际的字符串设置按钮的内容。我把Name看作是你例子中"button“的来源。在我的例子中,Name拉出了我绑定到我的datagrid的类名。
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();https://stackoverflow.com/questions/9295917
复制相似问题