WPF中动态创建模板

来源:互联网 发布:windows远程传输工具 编辑:程序博客网 时间:2024/05/19 02:23


            //内存中动态生成一个XAML,描述了一个DataTemplate
            XNamespace ns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
            XElement xDataTemplate =
            new XElement(ns + "DataTemplate", new XAttribute("xmlns", "http://schemas.microsoft.com/winfx/2006/xaml/presentation"),
             

                    new XElement(ns + "TextBlock", new XAttribute("Text", @"{Binding Path=.,StringFormat=N2}"))
           
            );
            //将内存中的XAML实例化成为DataTemplate对象,并赋值给
            //ListBox的ItemTemplate属性,完成数据绑定
            XmlReader xr = xDataTemplate.CreateReader();
           
            DataTemplate dataTemplate = XamlReader.Load(xr) as DataTemplate;

原创粉丝点击