来源:How to I access an attached property in code behind?

 

Q:

 

I have a rectangle in my XAML and want to change its Canvas.Left property in code behind:

 

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

 

 

But this doesn't work:

 

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

 

Does anyone know what the syntax is to do this?

A

Canvas.SetLeft(theObject, 50)

 

相关文章:

  • 2022-12-23
  • 2021-06-23
  • 2021-08-15
  • 2022-12-23
  • 2021-10-04
  • 2022-12-23
  • 2021-06-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-08-24
  • 2021-10-06
  • 2021-06-19
  • 2021-12-21
相关资源
相似解决方案