在 WPF 中以编程方式更改元素的位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1123101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing Position of an Element Programmatically in WPF
提问by azamsharp
I did not knew that this simple thing would be slightly complicated. I have a Canvas in which I am trying to add Ellipse dynamically. Here is the code:
没想到这个简单的事情会稍微复杂一点。我有一个 Canvas,我试图在其中动态添加 Ellipse。这是代码:
<StackPanel>
<Canvas Name="canvas" Background="LightBlue" Margin="5" Width="250" Height="250">
</Canvas>
<Button Content="Draw Images" Click="Button_Click" Width="100" Margin="10" />
</StackPanel>
And here is the code behind:
这是背后的代码:
private void Button_Click(object sender, RoutedEventArgs e)
{
Ellipse ellipse = new Ellipse();
ellipse.Fill = Brushes.Red;
ellipse.Width = 10;
ellipse.Height = 10;
ellipse.SetValue(Canvas.LeftProperty,100);
ellipse.SetValue(Canvas.TopProperty,100);
canvas.Children.Add(ellipse);
}
For some reason it throws the exception that 100 is not a valid value!
出于某种原因,它会抛出 100 不是有效值的异常!
回答by azamsharp
Here is the answer:
这是答案:
Canvas.SetLeft(ellipse,GetRandomValue());
Canvas.SetTop(ellipse,GetRandomValue());
回答by Ray
The reason 100
doesn't work is that SetValue()
interprets it as an integer, but Canvas.Top
& Canvas.Left
are doubles. Try 100d
instead. Also SetLeft()
& SetTop()
work because they expect doubles.
原因100
不起作用是将其SetValue()
解释为整数,但Canvas.Top
&Canvas.Left
是双精度数。试试吧100d
。还SetLeft()
与SetTop()
工作,因为他们期望双打。
回答by AndyW360
The code below works:
下面的代码有效:
ellipse.SetValue(Canvas.LeftProperty,100.0);
ellipse.SetValue(Canvas.TopProperty,100.0);
The values are of the double type.
值是双精度型。
回答by George Birbilis
The Left and Top are attached properties owned by Canvas class, that you can attach to any DependencyObject, whether it is a FrameworkElement or not and whether it is hosted in a Canvas or not.
Left 和 Top 是 Canvas 类拥有的附加属性,您可以附加到任何 DependencyObject,无论它是否是 FrameworkElement 以及它是否托管在 Canvas 中。
That's why you have to use:
这就是为什么你必须使用:
myDependencyObject.SetValue(Canvas.LeftProperty, leftValue); myDependencyObject.SetValue(Canvas.TopProperty, topValue);
myDependencyObject.SetValue(Canvas.LeftProperty, leftValue); myDependencyObject.SetValue(Canvas.TopProperty, topValue);
Most other containers, say the Grid will just ignore those property values, if our dependency object is a FrameworkElement contained in them instead of inside a Canvas. One could make though other containers that respect those properties
大多数其他容器,比如 Grid 将忽略这些属性值,如果我们的依赖对象是包含在其中而不是在 Canvas 中的 FrameworkElement。可以制作其他尊重这些属性的容器
回答by Sayed Mohammad Amin Emrani
if you want to move your canvas with matrix you should do like this :
如果你想用矩阵移动你的画布,你应该这样做:
<Canvas Name="mcanvas" >
<Canvas.RenderTransform>
<MatrixTransform x:Name="mt"/>
</Canvas.RenderTransform>
</Canvas>
then you can do all works you want, on Matrix "mt". Like this :
那么你就可以在 Matrix "mt" 上做你想做的所有工作。像这样 :
For Scale:
对于规模:
Matrix matrix = new Matrix();
matrix.Scale(1.5, 1.5);
mt.Matrix = matrix;
mcanvas.LayoutTransform = Transform.Identity;
For Translate ( Changing Position ) :
对于翻译(改变位置):
Matrix matrix = new Matrix();
matrix.Translate(50, 0);
mt.Matrix = matrix;
mcanvas.LayoutTransform = Transform.Identity;
And if you want to create a canvas element programmatically, you should do like this :
如果你想以编程方式创建一个画布元素,你应该这样做:
Ellipse el = new Ellipse();
Matrix matrix = new Matrix();
matrix.Translate(50, 0);
matrix.Scale(1.5,1.5);
el.RenderTransform = new MatrixTransform(matrix);
Hope this helps you.
希望这对你有帮助。
回答by Bobo
Try to convert integer type to Double type using "CDbl" function
尝试使用“CDbl”函数将整数类型转换为双精度类型
Try this:
尝试这个:
ellipse.SetValue(Canvas.LeftProperty,Cdbl(100));