Silverlight

52.SilverLight3 - AttachedProperty

Godffs 2009. 12. 2. 08:55
반응형
AttachedProperty
- 부모요소에서 상속받은 속성 사용시  붙임(Attached) 속성 사용
- SetValue() : 설정하기
- GetValue() : 가져오기

MainPage.xaml

[그림 52-1]


<Canvas Background="red" Width="400" Height="300" x:Name="LayoutRoot">

        <Rectangle x:Name="rect" Width="50" Height="50" Fill="Yellow" Stroke="Blue"

               StrokeThickness="1" Canvas.Left="100" Canvas.Top="50">

        </Rectangle>

</Canvas>


MainPage.xaml

public MainPage()

{

        InitializeComponent();

 

        //

        this.rect.MouseLeftButtonDown+= new

                  MouseButtonEventHandler(rect_MouseLeftButtonDown);

}

 

privatebool flag; // true/false ()

void rect_MouseLeftButtonDown(object sender, MouseButtonEventArgse)

{

        // : GetValue

        Brushc = this.LayoutRoot.Background;

 

        //

        Brushr = this.rect.Fill;

 

        //

        this.LayoutRoot.Background= r;

 

        //

        this.rect.Fill= c;

 

        //[!] set/get

        //<RectangleCanvas.Left=""/>

        intx = Convert.ToInt32(rect.GetValue(Canvas.LeftProperty));

 

        if(flag == true) // 100px

        {

                // -100

                this.rect.SetValue(Canvas.LeftProperty, Convert.ToDouble(x- 100));

        }

        else// 100px

        {

                // +100

                this.rect.SetValue(Canvas.LeftProperty, Convert.ToDouble(x+ 100));

        }

}


결과화면

[그림 52-2]



반응형