c# - WPF: Add controls from code -
i'm used windows forms not wpf. think wpf has great advantages i'm trying use in new projects.
my problem next: have xml file witch tells me controls have add form xml change need to:
- read xml file (solved, no problem)
- create custom wpf form data have read.
is possible? or should use windows forms?
yes possible.
wpf offers several ways of creating controls either in xaml or in code.
for case if need dynamically create controls, you'll have create them in code. can either create control directly using constructors in:
// create button. button mybutton= new button(); // set properties. mybutton.content = "click me!"; // add created button created container. mystackpanel.children.add(mybutton);
or create controls string containing xaml , use xamlreader parse string , create desired control:
// create stringbuilder stringbuilder sb = new stringbuilder(); // use xaml declare button string containing xaml sb.append(@"<button xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' "); sb.append(@"content='click me!' />"); // create button using xamlreader button mybutton = (button)xamlreader.parse(sb.tostring()); // add created button created container. stackpanel.children.add(mybutton);
now 1 of 2 methods want use depends on you.
jean-louis
Comments
Post a Comment