This article example describes how to dynamically load controls in XAML in WPF. Share it for your reference, as follows:
using System; using ; using ; using ; using ; using ; using ; using ; using ; using ; using ; using ; using ; using ; using ; using ; /* * Function: Test the dynamic loading of controls in XAML in WPF * and added to the specified child node. * Author: Kagula * Time: 2012-09-20 * Environment: VS2008 .NET FRAMEWORK 3.5 * Reference: [1] "Application=Code+Markup Reading Notes 19" * /15123181/viewspace-423015 * [2] "Pack URIs in Windows Presentation Foundation" * /en-US/library/aa970069(v=vs.90) */ namespace testXAMLLoad { /// <summary> /// Interaction logic for /// </summary> public partial class Window1 : Window { public Window1() { InitializeComponent(); //LoadEmbeddedXaml(); //LoadEmbeddedXaml2(); LoadEmbeddedXaml3(); } //Load from string public void LoadEmbeddedXaml() { Title = "Load Embedded Xaml"; string strXaml = "<Button xmlns='/winfx/2006/xaml/presentation'" + " Foreground='LightSeaGreen' FontSize='16pt' Width='128' Height='32'>" + " From String Object!</Button>"; StringReader strreader = new StringReader(strXaml); XmlTextReader xmlreader = new XmlTextReader(strreader); object obj = (xmlreader); ((UIElement)obj); } //Load Button control from external file public void LoadEmbeddedXaml2() { XmlTextReader xmlreader = new XmlTextReader("d:\\"); UIElement obj = (xmlreader) as UIElement; ((UIElement)obj); } //Load from resource file public void LoadEmbeddedXaml3() { //Build Action = Resource,Do not Copy, no corresponding cs file Uri uri = new Uri("/",); Stream stream =(uri).Stream; //FrameworkElement inherits from UIElement FrameworkElement obj =(stream) as FrameworkElement; (obj); } } }
List in xaml file
<Button xmlns='/winfx/2006/xaml/presentation' Foreground='LightSeaGreen' FontSize='16pt' Width='128' Height='32'> From File Object! </Button>
For more information about C# related content, please check out the topic of this site:Summary of thread usage techniques for C# programming》、《Summary of C# operation skills》、《Summary of XML file operation skills in C#》、《Tutorial on the usage of common C# controls》、《Summary of WinForm control usage》、《C# data structure and algorithm tutorial》、《Summary of C# array operation skills"and"Introduction to C# object-oriented programming tutorial》
I hope this article will be helpful to everyone's C# programming.