我有一个矩的在我的馈想改变它的 Canvas.Left 酒店代码:

<UserControl x:Class="Second90.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="300" KeyDown="txt_KeyDown">
    <Canvas>
        <Rectangle 
            Name="theObject" 
            Canvas.Top="20" 
            Canvas.Left="20" 
            Width="10" 
            Height="10" 
            Fill="Gray"/>
    </Canvas>
</UserControl>

但是,这并不工作:

private void txt_KeyDown(object sender, KeyEventArgs e)
{
    theObject.Canvas.Left = 50;
}

有没有人知道的语法做到这一点?

有帮助吗?

解决方案

Canvas.SetLeft(theObject, 50)

其他提示

试试这个

theObject.SetValue(Canvas.LeftProperty, 50d);

有一组的方法上。(基数WPF类),允许公共访问的所有依赖性。他们

  • 设定值
  • 获取名
  • ClearValue

编辑 更新后的设置使用双字,因为目标的类型是一倍。

由于我们正在改变“对象”的特性,这将是更好通过JaredPar使用方法suggedte:

theObject.SetValue(Canvas.LeftProperty, 50d);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top