DataTemplate template = new DataTemplate(); FrameworkElementFactory wrapPanel = new FrameworkElementFactory(typeof(WrapPanel)); FrameworkElementFactory textBlock = new FrameworkElementFactory(typeof(TextBlock)); textBlock.SetValue(TextBlock.TextProperty, "some text");//добавим текст wrapPanel.AppendChild(textBlock); template.VisualTree = wrapPanel; button.ContentTemplate = template;
The second option using XamlRedaer :
string temp = @"<DataTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""> <WrapPanel> <TextBlock/> </WrapPanel> </DataTemplate>"; StringReader stringReader = new StringReader(temp); XmlReader xmlReader = XmlReader.Create(stringReader) DataTemplate template = XamlReader.Load(xmlReader) as DataTemplate; button.ContentTemplate = template;
MVVMyou do not have enough ... - MihailPw