简体   繁体   中英

How to set inherited parent property for a Control Child on Code Behind

After I decide to which Container I will use to load my Controls, a Canvas. From my previous question.

I began to search on google about how to move, resize a UIElement on a Canvas. First article was written from Josh Smith , Dragging Elements in a Canvas .

When I finish to read it and tray the example. It wasn't what I was exactly looking for but it was very close. After I came across with the article from igkutikov , Dragging Elements in a Canvas , who had the same situation and made a very useful class who manage all methods to build a dragable canvas. Finally I decide to use this class and adapt to my project.

Tip: For who are learning how to build a canvas with dragable controls. It's hig recommended to read and study this 2 previous articles

In the article from igkutikov, to use the "DragCanvas.cs" class, we define the namespace who the class is placed. A continuation we implement in our project the DragCanvas who is a interface about Canvas .

<tools:DragCanvas x:Name="UCDragCanvas" Grid.Column="0" Background="Black">
        <Button Click="ButtonBase_OnClick" Content="Move me" Canvas.Left="30" Canvas.Top="40" tools:DragCanvas.CanBeDragged="True"></Button>
    </tools:DragCanvas>

So, in the example above, when we add a Button, we can set the Canvas.Left, Canvas.Top and DragCanvas.CanBeDragged properties, that are properties from his parent. This parent is our UCDragCanvas Container.

My question now is, how I can do it from code behind?

Button btn = new Button();
btn.Content = "Move me";
// ToDo add UCDragCanvas Properties for btn.
UCDragCanvas.Children.Add(btn);

Thank you very much and greetings!

Canvas.SetLeft(btn, 30);
...
DragCanvas.SetCanBeDragged(btn,true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM