WPF中动态创建模板


            //内存中动态生成一个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;

你可能感兴趣的:(WPF中动态创建模板)