글 작성자: 써니루루


이번에는 Silverlight의 컨트롤을 정의해서 만들어봅니다.

예전에 강의들을 때 작성하고 다시보니 무슨소린지 모르겠네요.. ㅋㅋㅋ

그래도 전혀 무지한 것 보단 캡쳐라도 약간 있으니 ㅠㅠ 나중에 약간이라도 도움될 듯..

부족하지만 1%라도 얻을게 있을지 모르니 정리 안된 상태로 등록합니다... ㅋㅋ







 

 

 

 

Generic.xaml

  • Custom Control의 기본 모양을 정의하는 파일
  • Xmlns와 xmlns:x는 page.xaml에서 가져온다

 

<ResourceDictionary

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

 

 

 

</ResourceDictionary>

 

 

 

public class MyControl : Control

{

public MyControl()

{

this.DefaultStyleKey = typeof(MyControl);

}

}

 

 

Control을 상속 받고

기본 스타일 키를 현재 형식의 타입을 지정한다.

 

 

 

 

 

재정의할 메소드가 있는데, OnApplyTemplate 을 override 하여 메소드를 만든다

 

 

public override void OnApplyTemplate()

{

base.OnApplyTemplate();

}

 

 

이제 Generic.xaml에서 내가 작성하는 control의 네임스페이스를 지정한다.

 

my로 prefix를 주었으므로 my를 앞으로 사용하게 된다

<ResourceDictionary

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

 

xmlns:my="clr-namespace:CustomControlTest2"

 

>

 

<Style TargetType="my:MyControl">

 

</Style>

 

</ResourceDictionary>

 

 

이제 컨트롤의 템플릿을 만든다

 

<ResourceDictionary

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

 

xmlns:my="clr-namespace:CustomControlTest2"

 

>

 

<Style TargetType="my:MyControl">

<Setter Property="Template">

<Setter.Value>

<ControlTemplate TargetType="my:MyControl">

 

<Grid>

<TextBlock x:Name="MyText" Text="안녕?" />

</Grid>

 

</ControlTemplate>

</Setter.Value>

</Setter>

</Style>

 

</ResourceDictionary>

 

 

 

이제 기본적인 내용은 끝났고 Grid 안에 내용을 지정한다.

 

 

이제 컨트롤에서 속성을 노출할 수 있도록 작성해보자.

propdp를 입력하면 인텔리센스로 작성할 수 있다.

 

마지막에 UIxxxxxxx(0) 라고 나오는 부분은 지워주고 PropertyMetadata 로 변경해야한다.

 

using System;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Ink;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

 

namespace CustomControlTest2

{

public class MyControl : Control

{

TextBlock _tb = null;

 

public string Text

{

get { return (string)GetValue(TextProperty); }

set { SetValue(TextProperty, value); }

}

 

// Using a DependencyProperty as the backing store for Text. This enables animation, styling, binding, etc...

public static readonly DependencyProperty TextProperty =

DependencyProperty.Register(

"Text",

typeof(string),

typeof(MyControl),

new PropertyMetadata(new PropertyChangedCallback(

// e는 보낸쪽

(d, e) =>

{

MyControl myControl = d as MyControl;

if (myControl != null && myControl._tb != null)

{

myControl._tb.Text = e.NewValue.ToString();

}

})

)

);

 

 

public MyControl()

{

this.DefaultStyleKey = typeof(MyControl);

}

 

public override void OnApplyTemplate()

{

base.OnApplyTemplate();

 

// 컨트롤에 들어 있는 TextBlock을 가져온다.

_tb = GetTemplateChild("MyText") as TextBlock;

 

if (_tb !=null)

{

_tb.Text = "커스텀 컨트롤~!";

}

}

}

}

 

 

 

 

 

 

 

이제 Page.xaml에 돌아와서 MyControl을 작성해 보자

 

<UserControl x:Class="CustomControlTest2.Page"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

 

xmlns:my="clr-namespace:CustomControlTest2"

 

Width="400" Height="300">

<Grid x:Name="LayoutRoot" Background="White">

<my:MyControl Text="여기에 글을 작성해보자!!"></my:MyControl>

</Grid>

</UserControl>