코드 비하인드에서 붙임(Attached) 속성 사용
.NET프로그래밍/Silverlight 3.0 2009. 12. 2. 09:30 |* 붙임속성을 사용할 때는 반드시 GetValue와 SetValue를 사용해야한다.
[MainPage.xaml]
<UserControl x:Class="RiaAttachedProperty.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Canvas x:Name="LayoutRoot" Background="Yellow" Width="400" Height="300">
<Rectangle x:Name="rect" Width="50" Height="50" Fill="Red"
Stroke="Blue" StrokeThickness="1"
Canvas.Left="100" Canvas.Top="50"
></Rectangle>
</Canvas>
</UserControl>
[MainPage.xaml.cs]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace RiaAttachedProperty
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
// 이벤트 등록
this.rect.MouseLeftButtonDown += new MouseButtonEventHandler(rect_MouseLeftButtonDown);
}
private bool flag; // 기본값은 false. 한번 클릭하면 true, 다시 한번 클릭하면 false.
void rect_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
// 캔버스의 배경색을 가져오기 : GetValue
Brush c = this.LayoutRoot.Background;
// 사각형의 배경색을 가져오기
Brush r = this.rect.Fill;
// 캔버스의 배경색을 설정하기 : SetValue
this.LayoutRoot.Background = r;
// 사각형의 배경색을 설정하기
this.rect.Fill = c;
//[!] 붙임 속성 set/get
int x = Convert.ToInt32(rect.GetValue(Canvas.LeftProperty)); // <Rectangle Canvas.Left="값" />
if (flag == true) // 왼쪽으로 100픽셀 이동
{
this.rect.SetValue(Canvas.LeftProperty, Convert.ToDouble(x - 100)); // 현재좌표값 - 100
}
else // 오른쪽으로 100픽셀 이동
{
this.rect.SetValue(Canvas.LeftProperty, Convert.ToDouble(x + 100)); // 현재좌표값 + 100
}
flag = !flag; // 토글링
}
}
}
'.NET프로그래밍 > Silverlight 3.0' 카테고리의 다른 글
RepeatButton 컨트롤 (0) | 2009.12.02 |
---|---|
HyperlinkButton 컨트롤 (0) | 2009.12.02 |
이벤트 라우팅(Event Routing) (0) | 2009.12.01 |
InkPresenter : 잉크 (0) | 2009.12.01 |
드래그 앤 드롭 (0) | 2009.12.01 |