ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • WPF에서 기존 control을 상속받아 새로운 control 구현하기
    WPF 2007. 12. 24. 12:44
    이전 블로그에 있던 글입니다.     글쓴 시간 : 2007-01-04 18:34:29.0


    이 부분은 UserControl이나 CustomControl과는 조금 다르다.

    UserControl이나 CustomControl은 여러가지 컨트롤을 합쳐서 새로운 컨트롤을 만든다고 생각하면되고, 또는 전혀 새로운 컨트롤을 제작한다고 생각하면된다


    하지만 UserControl이나 CustomControl과는 달리 비쥬얼 스튜디오에서 만드는걸 지원하지 않는다.

    그럼 어떻게 구성하면 될까?

    나만의 방법일지 모르지만


    우선 그냥 C# 클래스파일(.cs)를 작성한다.

    그리고 원하는 컨트롤 클래스를 상속 받는다.

    ex)
    public class MyButton : Button
        {
            public MyButton()
                : base()
            {
                Style style = new Style();
                style.TargetType = typeof(MyButton);

                Setter setter = new Setter(MyButton.HeightProperty, 30.0);
                Setter setter2 = new Setter(MyButton.WidthProperty, 30.0);

                style.Setters.Add(setter);
                style.Setters.Add(setter2);

                this.Style = style;
            }
        }

    버튼을 상속한 예제이다.
    이름은 MyButton이고, 2가지 스타일( 넓이, 높이)을 주었다.


    이렇게 제작된 컨트롤을 사용하는 방법

    ex)
    <Window x:Class="WindowsApplication1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:MyListbox;assembly=MyListBox"
        Title="WindowsApplication1" Height="300" Width="300"
        >
        <Grid>
          <local:UserControl1></local:UserControl1>
        </Grid>
    </Window>

    이다..  보다시피 <local:UserControl1>이라고 명시해서 사용하고 있다.

    이 컨트롤과 버튼의 차이점은 위에서 준 2개의 스타일이다.

Designed by Tistory.