'DependencyProperty'에 해당되는 글 2건

  1. 2009/07/03 Silverlight – Custom control
  2. 2009/06/23 Silverlight Custom control
2009/07/03 00:25

Silverlight – Custom control


이번엔 실버라이트 사용자 컨트롤을 만들어봅니다.

설명이 많이 부족하지만 따라하시다보면 성공하실수.....

사실 제가 부족해서 설명이 많이 부족하네요ㅠㅠ



 

 

 

 

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>

저작자 표시 비영리 변경 금지
이올린에 북마크하기(0) 이올린에 추천하기(0)
크리에이티브 커먼즈 라이선스
Creative Commons License

'.NET > Silverlight' 카테고리의 다른 글

Silverlight - Network Sample 2  (0) 2009/07/15
Silverlight - Network Example 1  (0) 2009/07/13
Silverlight – Custom control  (0) 2009/07/03
Silverlight Custom control  (0) 2009/06/23
Silverlight – bubbling events  (2) 2009/05/18
Silverlight – DispatcherTimer , Clock  (0) 2009/05/12
올블로그추천버튼 블코추천버튼 블로그뉴스추천버튼 믹시추천버튼 한RSS추가버튼 구글리더기추천버튼


이 포스팅이 도움이 되었다면 구글에서 관련 정보를 찾아 보세요 ^^


Trackback 0 Comment 0

Trackback : http://i-ruru.com/trackback/459 관련글 쓰기

2009/06/23 20:21

Silverlight Custom control


이번에는 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>

저작자 표시 비영리 변경 금지
이올린에 북마크하기(0) 이올린에 추천하기(0)
크리에이티브 커먼즈 라이선스
Creative Commons License

'.NET > Silverlight' 카테고리의 다른 글

Silverlight - Network Example 1  (0) 2009/07/13
Silverlight – Custom control  (0) 2009/07/03
Silverlight Custom control  (0) 2009/06/23
Silverlight – bubbling events  (2) 2009/05/18
Silverlight – DispatcherTimer , Clock  (0) 2009/05/12
Silverlight – Dynamic Page load  (0) 2009/05/10
올블로그추천버튼 블코추천버튼 블로그뉴스추천버튼 믹시추천버튼 한RSS추가버튼 구글리더기추천버튼


이 포스팅이 도움이 되었다면 구글에서 관련 정보를 찾아 보세요 ^^


Trackback 0 Comment 0

Trackback : http://i-ruru.com/trackback/458 관련글 쓰기