A
A
Anton2017-10-28 00:58:11
WPF
Anton, 2017-10-28 00:58:11

WPF XAML template?

Hello. I have some set of elements that should be generated by the code. Is it possible in wpf to create a xaml file that describes the necessary elements, and to be able to generate this xaml many times in some part of the window?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Smilley, 2017-10-28
@Smilley

What is the problem? You have a parent element, you have controls generated by the code, add to the collection of controls of the parent object those controls that you get from the code

Z
Zakharov Alexander, 2017-10-28
@AlexZaharow

Yes, you can. It is necessary to add a new type to the project, called "control". Let's call it MyControl.xaml. It is also a XAML type and is edited like a normal window. There you can create any GUI structure you need (buttons, checkboxes, lists, that's all). Then you return to the code of the main window, in which you need to add this control. Create a new Control programmatically:
MyControl ctrl = new MyControl();
this(or window subcontrol).Children.Add(ctrl);
After that, your new control will appear in the main window at runtime.
You can also add a new control visually, but in my experience, Visual Studio before the 2017 version is reluctant to work in the visual editor with custom controls, it is a lot buggy and sometimes even requires Visual Studio to be restarted. I have had 2017 not so long ago, until I had a chance to check for this glitch.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question