IT

코드 숨김에서 어떤 속성에 액세스하는 방법은 무엇입니까?

lottoking 2020. 8. 29. 21:35
반응형

코드 숨김에서 어떤 속성에 액세스하는 방법은 무엇입니까?


XAML에 사각형이 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);

모든 속성에 대한 공통 액세스를 허용하는 DependencyObject (대부분의 WPF 클래스 기반)에 대한 메소드 그룹이 있습니다. 그들

  • SetValue
  • GetValue
  • ClearValue

편집 대상 유형이 double 형 이중 리터럴을 사용하도록 설정을 업데이트했습니다.


'객체'의 속성을 변경하고 있으므로 JaredPar에서 제안한 메소드를 사용하는 것이 좋습니다.

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

참고 URL : https://stackoverflow.com/questions/541420/how-to-i-access-an-attached-property-in-code-behind

반응형