2021년 11월 20일 토요일

(WPF교육, WPF동영상)WPF 프로퍼티 개요 및 컨텐트 프로퍼티. 학원가지말고혼자해보세요.

 (WPF교육, WPF동영상)WPF 프로퍼티 개요 및 컨텐트 프로퍼티. 학원가지말고혼자해보세요.

http://ojc.asia/bbs/board.php?bo_table=WPF&wr_id=160 


WPF 프로퍼티 개요 및 컨텐트 프로퍼티

WPF 프로퍼티 개요 및 컨텐트 프로퍼티WPF 객체, 엘리먼트들은 프로퍼티를 가지고 있는데 이것에 속성값을 부여해서 요소의 상태를 표현할 수 있다.다음 XAML 코드는 버튼을 표시하고 세 개의 프로

ojc.asia

https://www.youtube.com/watch?v=FJbQJxtMnVU&list=PLxU-iZCqT52Cmj47aKB1T-SxI33YL7rYS&index=9&t=1s 

  • WPF 객체, 엘리먼트들은 프로퍼티를 가지고 있는데 이것에 속성값을 부여해서 요소의 상태를 표현할 수 있다.
  • 다음 XAML 코드는 버튼을 표시하고 세 개의 프로퍼티 값을 설정하고 있다.

<Button Foreground="LightSeaGreen"

        FontSize="20 pt"

        Content="Click Me!"

/>


아래와 같은 “프로퍼티 엘리먼트”로도 표현 가능하다.

<Button FontSize="20 pt"

        Content="Click Me!">

<Button.Foreground>

       LightSeaGreen

    </Button.Foreground>

</Button>


<Button FontSize="20 pt"

        ForeGround="LightSeaGreen">

<Button.Content>

       Click Me!

    </ Button.Content>

</Button>


아래처럼 Button.Content 태그는 생략 가능하다.

<Button FontSize="20 pt"

        ForeGround="LightSeaGreen">

Click Me!

</Button>


다음과 같은 표현 역시 가능하다.

<Button>

<Button.Foreground>

       LightSeaGreen

    </Button.Foreground>

   

    Click Me!


    <Button.ForeSize>

       20 pt

    </Button.ForeSize>

</Button>


컨텐트 프로퍼티는 본 바와 같이 좀 특별하다. XAML에서 ContentControl을 상속한 클래스는 컨텐트 프로퍼티로 인식되는 특별한 하나의 프로퍼티가 있는데 Button의 경우 컨텐트 프로퍼티는 “Content”이다.


컨텐트 프로퍼티로 인식되는 프로퍼티는 System.Window.Serialization 네임스페이스에 정의된 ContentProperty Attribute에 의해 정의된다. PresentationFramework.dll 파일에 Button 클래스는 다음과 같이 정의되어 있다.


[ContentPropery(“Content”)]  //어트리뷰트 선언

public class Button { }


<Button> 태그 사이의 값을 Button의 Content 프로퍼티에 넣어 준다는 의미임.


StackPanel의 ContentProperty 어트리뷰트는 다음과 같이 정의되어 있다.

[ContentPropery(“Children”)]  //어트리뷰트 선언

public class StackPanel { }


이 ContentPropery 어트리뷰트는 StackPanel의 자식을 StackPanel 엘리먼트의 자식으로 포함하게 해준다. StackPanel 태그 사이의 요소들을 Children 프로퍼티에 넣어 준다는 이야기이다. 다음 예문을 보자.


<StackPanel Name=”MyStackPanel”>

<Button>Button 1</Button>

<TextBlock>Middle TextBlock</TextBlock>

    <Button>Button 2</Button>

</StackPanel>


C# 코드에서 넣으려면 다음과 같이 하면 된다.

stackPanel.Children.Add(new Button

                {

                    Content=”Button 1”

                });

……


3.2 컨텐트 프로퍼티 덤프 예제


  • 비주얼 스튜디오 -> WPF 응용프로그램 , 프로젝트명 : DumpContentProperty

App.xaml, MainWindow.xaml 삭제

  • 추가 -> 새항목 -> C# 클래스, 이름 : DumpContentProperty

생성 후 우측 솔루션 탐색기의 Properties 더블 클릭 후 출력형태를 콘솔 응용 프로그램으로 설정.



[DumpContentProperty.cs]

using System;

using System.Collections.Generic;

using System.Reflection;

using System.Windows;

using System.Windows.Markup;


namespace DumpContentProperty

{

    class DumpContentProperty

    {

        [STAThread]

        public static void Main()

        {

            // PresentationCore, PresentationFramework 참조추가 되어 있어야 한다.

            UIElement dummy1 = new UIElement();

            FrameworkElement dummy2 = new FrameworkElement();


            // 클래스와 ContentProperty를 담을 SortedList 정의

            SortedList<stringstring> listClass = new SortedList<stringstring>();


           string strFormat = "{0,-35}{1}";


            // Loop through the loaded assemblies.

            foreach (AssemblyName asmblyname in

                        Assembly.GetExecutingAssembly().GetReferencedAssemblies())

            {

                // Loop through the types.

                foreach (Type type in Assembly.Load(asmblyname).GetTypes())

                {

                    // Loop through the custom attributes.

                    // (Set argument to 'false' for non-inherited only!)

                    foreach (object obj in type.GetCustomAttributes(

                                            typeof(ContentPropertyAttribute), true))

                    {

                        // Add to list if ContentPropertyAttribute.

                        if (type.IsPublic && obj as ContentPropertyAttribute != null)

                            listClass.Add(type.Name,

                                          (obj as ContentPropertyAttribute).Name);

                    }

                }

            }

            // Display the results.

            Console.WriteLine(strFormat, "Class""Content Property");

            Console.WriteLine(strFormat, "-----""----------------");


            foreach (string strClass in listClass.Keys)

                Console.WriteLine(strFormat, strClass, listClass[strClass]);

            Console.ReadKey();

        }

    }

}


[실행 결과]


 

#WPF프로퍼티#컨텐트프로퍼티. #WPF교육#WPF강의#WPF강좌#WPF학원 

WPF프로퍼티컨텐트프로퍼티. WPF교육WPF강의WPF강좌WPF학원 

WPF교육, WPFXAML, WPF동영상, XAML, XAML강좌, WPF메인, WPF

WPF교육, WPF동영상, Main 메소드 없이 XAML을 만들기


https://www.youtube.com/watch?v=KfY6DqWtcqs&list=PLxU-iZCqT52Cmj47aKB1T-SxI33YL7rYS&index=5&t=22s 

  • 윈도우나 다른 엘리먼트, 컨트롤을 위한 XAML 파일을 만드는 것 이외에 Application 객체를 위한 XAML 파일을 만들고 Main 메소드를 작성하지 않는 것이 보편적인 방법이다. 
  • 비주얼 스튜디오 -> WPF 응용프로그램 , 프로젝트명 : NoMainXaml

http://ojc.asia/bbs/board.php?bo_table=WPF&wr_id=159 


WPF교육, WPF동영상, Main 메소드 없이 XAML을 만들기

WPF교육, WPF동영상, Main 메소드 없이 XAML을 만들기윈도우나 다른 엘리먼트, 컨트롤을 위한 XAML 파일을 만드는 것 이외에 Application 객체를 위한 XAML 파일을 만들고 Main 메소드를 작성하지 않는 것이

ojc.asia

App.xaml, MainWindow.xaml 삭제

  • 프로젝트에서 마우스 우측버튼 -> 추가 -> 새항목 -> WPF -> 창(Window) -> MyWindow.xaml 생성

 

 

  • MyWindow.xaml

 

<Window x:Class="NoMainXaml.MyWindow"

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

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

        Title="MyWindow" 

        SizeToContent="WidthAndHeight"

        ResizeMode="CanMinimize">

    <Button HorizontalAlignment="Center"

            VerticalAlignment="Center"

            Margin="1.5in"

            Click="Button_Click">

        Click Me!        

    </Button>

</Window>

 

이 XAML 파일은 MyWindow.xaml.cs 파일에 정의되어 있는 MyWindow 클래스를 Partial 클래스로써 공유한다. MyWindow.xaml.cs의 생성자에서는 InitializeComponent를 호출하고 Button의 Click 이벤트를 위한 이벤트 핸들러(Button_Click)를 작성한다.

 

  • MyWindow.xaml.cs

 

using System.Windows;

using System.Windows.Controls;


namespace NoMainXaml

{

public partial class MyWindow : Window

    {

        public MyWindow()

        {

            InitializeComponent();

        }


        private void Button_Click(object sender, RoutedEventArgs e)

        {

            Button b = sender as Button;

            MessageBox.Show("Button " + b.Content);

        }

    }

}



  • 이번에 작성할 XAML 파일은 Application 객체를 위한 것으로 이름은 MyApplication.xaml 이다.

 

  • 프로젝트에서 마우스 우측버튼 -> 추가 -> 새항목 -> WPF -> 창(Window) -> MyApplication.xaml 생성 후 속성 창에서 빌드작업 : ApplicationDefinition으로 설정.




 

[MyApplication.xaml]

<Application x:Class="NoMainXaml.MyApplication"

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

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

        StartupUri="MyWindow.xaml" />

 

[MyApplication.xaml.cs]

using System.Windows;


namespace NoMainXaml

{

    public partial class MyApplication : Application

    {

        public MyApplication()

        {

        }

    }

}

 

  • StatupUri 속성은 어플리케이션이 구동되기 전에 MyWindow.xaml 파일은 MyWindow.baml로 컴파일되고 응용프로그램 리소스를 생성하지만 이는 단지 응용프로그램이 띄울 초기 Window 객체 자체선언에 지나지 않는다. 따라서 StratupUri는 메인함수에서 호출하는 Run() 메소드를 대체해서 초기 Window를 띄운다.

 

  • MyApplication.xaml.cs 파일은 XAML 파일 속성에 이벤트 핸들러 메소드가 있다면 이 파일 안에서 그 메소드를 구현하면 된다.

 

  • 이 프로그램을 컴파일 하고나서 obj -> debug안의 MyApplication.g.cs 파일에 Main 메소드가 생성되어 있는 것을 확인해 보자.

 

public static void Main() {

            NoMainXaml.MyApplication app = new NoMainXaml.MyApplication();

            app.InitializeComponent();

            app.Run();

        }

 

  • 실행 화면 



#WPF교육, #WPFXAML, #WPF동영상, #XAML, #XAML강좌, #WPF메인, #WPF

WPF교육, WPFXAML, WPF동영상, XAML, XAML강좌, WPF메인, WPF 

(C#교육동영상)C# ADO.NET 실습 ODP.NET/ODAC 설치 오라클 함수 호출 실습, C#학원, WPF학원, 닷넷학원, 자바학원

  (C#교육동영상)C# ADO.NET 실습  ODP.NET/ODAC 설치  오라클 함수 호출 실습, C#학원, WPF학원, 닷넷학원, 자바학원 https://www.youtube.com/watch?v=qIPU85yAlzc&list=PLxU-i...