icsharpcode / WpfDesigner

The WPF Designer from SharpDevelop
MIT License
949 stars 251 forks source link

How do i add controls to my design surface programmatically? #89

Closed Jerry2306 closed 2 years ago

Jerry2306 commented 2 years ago

I want to add UserControls to my design surface programmatically. First i tried adding it to the children UIElementCollection for my root control (which is a grid). When i've done this i couldn't select the control on the surface, but it appeared. Then i tried it via the CreateComponentTool. With this i can select the control on the surface and move it. But the control is positioned outside my design panel (i think the design panel is the root document, were i can order my controls on and the design surface is the whole design control, containing the design panel?): image

As well the control must have VerticalAlignment->Bottom and HorizontalAlignment->Right. But i want it to be from Top, Left: image

I made the AddItemsWithDefaultSize method (in CreateCompnentTool) public so i can create my DesignItem and can pass it directly down, instead of letting the CreateComponentTool do this (i want to manipulate attributes of my obj). I have tried it as follows: image

My UserControl should not be the problem as it is added right via the toolbox.

Thanks in advance and best regards Jeremy

jogibear9988 commented 2 years ago

I create the instance of the item I'd like to add. Register it as designItem via .RooItem.Services.Component.Register... And then I add this DesignItem to the root Item

Jerry2306 commented 2 years ago

@jogibear9988 Thank you very much! If I'm creating my DesignItem via the "RegisterComponentForDesigner" method instead of the "CreateComponentTool.CreateItem" it works like a charm.

For whoever is interested how i solved it: grafik