[英]How to change a value of UIElement
I have a WPF application and Canvas in there. 我有一个WPF应用程序和Canvas。 In Canvas I have a Rectangle. 在画布中我有一个矩形。 How can I change his properties, like Height or Width while program is already processing? 如何在程序处理过程中更改其属性,如高度或宽度? Something like: 就像是:
int index = 0;
var childByIndex = canvas.Children[index];
childByIndex.SetValue(Height, 15);
你必须告诉你想要设置哪种类型的dp,如下所示:
((Rectangle)canvas.Children[index]).SetValue(Rectangle.HeightProperty, 15.0);
The easiest way would be to give your rectangle a name in XAML and then use it in your code: 最简单的方法是在XAML中为矩形命名,然后在代码中使用它:
<Canvas>
<Rectangle x:Name="rect" />
</Canvas>
rect.Height = 15;
If for some reason you can't give your rectangle a name in XAML, you can cast your found object to Rectangle
before doing the operation: 如果由于某种原因您无法在XAML中为矩形指定名称,则可以在执行操作之前将找到的对象Rectangle
为Rectangle
:
Rectangle rect = (Rectangle)childByIndex;
rect.Height = 15;
If you're looking to change an attached property, like the location in the canvas, you can do it like this: 如果您要更改附加属性,例如画布中的位置,您可以这样做:
Canvas.SetTop(rect, 10);
Canvas.SetLeft(rect, 20);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.