简体   繁体   English

如何在背后的代码上设置控制子项的继承父属性

[英]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. 我开始在Google上搜索有关如何移动,调整Canvas上的UIElement大小的信息。 First article was written from Josh Smith , Dragging Elements in a Canvas . 第一篇文章是由乔什史密斯Josh Smith )撰写的“ 在画布上拖动元素”

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. 在看到igkutikov的文章“ 在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 建议阅读和研究前2篇文章

In the article from igkutikov, to use the "DragCanvas.cs" class, we define the namespace who the class is placed. 在igkutikov的文章中,要使用“ DragCanvas.cs”类,我们定义放置该类的名称空间。 A continuation we implement in our project the DragCanvas who is a interface about Canvas . 我们在项目中实现了DragCanvas的延续,DragCanvas是有关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. 因此,在上面的示例中, 当我们添加Button时,我们可以设置Canvas.Left,Canvas.Top和DragCanvas.CanBeDragged属性,这些属性来自其父级。 This parent is our UCDragCanvas Container. 此父级是我们的UCDragCanvas容器。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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