레이블이 StaticResource인 게시물을 표시합니다. 모든 게시물 표시
레이블이 StaticResource인 게시물을 표시합니다. 모든 게시물 표시

2022년 1월 21일 금요일

WPF 스타일(Style), TargetType, BasedOn, Setter, Static Resource, C#, WPF동영상

 WPF 스타일(Style), TargetType, BasedOn, Setter, Static Resource, C#, WPF동영상, C#학원, 닷넷학원, 자바학원, SQL학원, 닷넷교육, C#교육, 자바교육

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


WPF 스타일(Style), TargetType, BasedOn, Setter, Static Resource, C#, WPF동영상

WPF 스타일(Style), TargetType, BasedOn, Setter, Static Resource, C#, WPF동영상WPF StyleTargetType, BasedOnWPF 스타일은 여러 요소, 컨트롤에 일관된 UI를 적용할 수 있다.WPF 스타일은 의존속성과 그 속성값의 모임이라

ojc.asia


https://www.youtube.com/watch?v=K3v-A5d2fNI&list=PLxU-iZCqT52Cmj47aKB1T-SxI33YL7rYS&index=20 

https://www.youtube.com/watch?v=Qj26gdH651A&list=PLxU-iZCqT52Cmj47aKB1T-SxI33YL7rYS&index=22 


WPF Style

TargetType, BasedOn




 

  • WPF 스타일은 여러 요소, 컨트롤에 일관된 UI를 적용할 수 있다.
  • WPF 스타일은 의존속성과 그 속성값의 모임이라고 보면 되는데 대표적인 속성인 Setter는 의존속성과 그 값들을 가지고 있다.
  • WPF Style 클래스는 6개의 속성을 정의하는데 그 중 Setter, Resources는 이미 다루어 보았다. 스타일을 적용할 타겟 엘리먼트의 타입을 기술하는 TargetType이라는 속성도 있는데 중괄호 안에 x:Type이라는 마크업 확장과 적용을 원하는 Type 클래스의 이름을 명시한다. 
  • Target을 설정하면 x:Key 값을 명시할 필요가 없고 키는 TargetType을 통해 생성된다. x:key를 사용하지 않는다면 모든 하위 요소에 스타일이 적용되며 x:Key를 이용하면 키값을 이용하여 스타일을 적용하고픈 요소에서 정의해서 참조하면 된다.


<Style TargetType=”{x:Type TextBlock}” …>

</Style>


  • TargetType을 사용하면 Setter 엘리먼트 내에서 프로퍼티 이름을 완전히 나열할 필요가 없다.

<Setter Property=”Button.FontSize” Value=”12”/>  

>>>>

<Setter Property=”FontSize” Value=”12”/>


  • TargetType 예제


<Window x:Class="WpfApp6.MainWindow"

        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"

        xmlns:local="clr-namespace:WpfApp6"

        mc:Ignorable="d"

        Title="MainWindow" Height="350" Width="525">

    <StackPanel>

        <StackPanel.Resources>

            <Style TargetType="{x:Type Button}">

                <Setter Property="FontSize" Value="24" />

                <Setter Property="Foreground" Value="Blue" />

            </Style>


            <Style TargetType="{x:Type TextBlock}">

                <Setter Property="Foreground" Value="Red" />

            </Style>

        </StackPanel.Resources>


        <Button>

            Button 1

        </Button>


        <TextBlock>

        TextBlock 1

        </TextBlock>


        <Button>

            <TextBlock>

             Button with TextBlock Content

            </TextBlock>

        </Button>

    </StackPanel>

</Window>


  • 엘리먼트에는 오직 하나의 Style만 적용되는데 이 Style은 비주얼 트리에서 키로 검색된 최초의 Style 이거나 엘리먼트의 클래스가 TargetType과 일치하는 Style 이다.
  • 이미 만들어진 Style을 기반으로 약간의 프로퍼티 정의를 추가한 Style을 정의하는 것도 가능한데 이 경우 Style에서 기존 Style을 참조하기 위해 BasedOn 속성을 사용할 수  있다. 

<Window x:Class="WpfApp6.MainWindow"

        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"

        xmlns:local="clr-namespace:WpfApp6"

        mc:Ignorable="d"

        Title="MainWindow" Height="350" Width="525">

    <StackPanel>

        <StackPanel.Resources>

            <Style x:Key="norbtn">

                <Setter Property="Control.FontSize" Value="24" />

                <Setter Property="Control.Foreground" Value="Blue" />

                <Setter Property="Control.HorizontalAlignment" Value="Center" />

                <Setter Property="Control.Margin" Value="24" />

                <Setter Property="Control.Padding" Value="20, 10, 20, 10" />

            </Style>


            <Style x:Key="hotbtn" BasedOn="{StaticResource norbtn}">

                <Setter Property="Control.Foreground" Value="Red" />

            </Style>


        </StackPanel.Resources>


        <Button Style="{StaticResource norbtn}">

            Button Number 1

        </Button>


        <Button Style="{StaticResource hotbtn}">

            Button Number 2

        </Button>


        <Button Style="{StaticResource norbtn}">

            Button Number 3

        </Button>

    </StackPanel>

</Window>


  • TargetType 속성을 가진 기존 스타일을 기본으로 해서 정의하는 것도 가능하지만 조금 복잡해 진다.

BasedOn=”{StaticResource {x:Type Button}}”

  • 새로 정의한 Style에서는 스타일이 바탕으로 한 클래스나 그 클래스의 파생클래스로 TargetType을 설정해야 한다.

<Window x:Class="WpfApp6.MainWindow"

        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"

        xmlns:local="clr-namespace:WpfApp6"

        mc:Ignorable="d"

        Title="MainWindow" Height="350" Width="525">

    <StackPanel>

        <StackPanel.Resources>

            <Style TargetType="{x:Type Button}">

                <Setter Property="Control.FontSize" Value="24" />

                <Setter Property="Control.Foreground" Value="Blue" />

            </Style>


            두번째 정의하는 스타일은 충돌을 피하기 위해 x:Key 필요

            <Style x:Key="hotbtn"

               TargetType="{x:Type Button}"

               BasedOn="{StaticResource {x:Type Button}}">

                <Setter Property="Control.Foreground" Value="Red" />

            </Style>


        </StackPanel.Resources>


        <Button>

            Button Number 1

        </Button>


        <Button Style="{StaticResource hotbtn}">

            Button Number 2

        </Button>


        <Button>

            Button Number 3

        </Button>

    </StackPanel>

</Window>


 

  • TargetType을 사용하면 특정 타입 엘리먼트는 항상 특정 스타일을 가지며 클래스의 계층과 일치하는 스타일의 계층을 다음과 같이 정의할 수 있다.



<Window x:Class="WpfApp6.MainWindow"

        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"

        xmlns:local="clr-namespace:WpfApp6"

        mc:Ignorable="d"

        Title="MainWindow" Height="350" Width="525">

    <StackPanel>

        <StackPanel.Resources>

            <Style TargetType="{x:Type Control}">

                <Setter Property="Control.FontSize" Value="24" />

                <Setter Property="Control.Foreground" Value="Blue" />

            </Style>


            <Style TargetType="{x:Type Button}"

               BasedOn="{StaticResource {x:Type Control}}">

                <Setter Property="Control.Foreground" Value="Red" />

            </Style>


            <Style TargetType="{x:Type Label}"

               BasedOn="{StaticResource {x:Type Control}}">

                <Setter Property="Control.Foreground" Value="Green" />

            </Style>


            <Style TargetType="{x:Type TextBox}"

               BasedOn="{StaticResource {x:Type Control}}">

            </Style>

        </StackPanel.Resources>


        <Button>

            Button Control

        </Button>


        <Label>

            Label Control

        </Label>


        <TextBox>

            TextBox Control

        </TextBox>

    </StackPanel>

</Window>


 #WPF#WPF스타일#WPFSTYLE#TargetType#BasedOn#STYLESetter#StaticResource#WPF동영상#WPF교육#WPF학원#WPF강좌#닷넷교육WPFWPF스타일WPFSTYLETargetTypeBasedOnSTYLESetterStaticResourceWPF동영상WPF교육WPF학원WPF강좌닷넷교육,

2022년 1월 16일 일요일

(WPF동영상)WPF Style이란? Resource, StaticResource 이용 WPF 스타일 적용, WPF교육, C#교육, WPF학원, C#학원

 (WPF동영상)WPF Style이란?  Resource, StaticResource 이용 WPF 스타일 적용, WPF교육, C#교육, WPF학원, C#학원


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


(WPF동영상)WPF Style이란? Resource, StaticResource 이용 WPF 스타일 적용

(WPF동영상)WPF Style이란? Resource, StaticResource 이용 WPF 스타일 적용WPF Style이란?로컬 및 윈도우 스타일 적용WPF Style 이란?WPF XAML의 Resource절은 객체를 참조할 때도 사용되지만 Style 객체를 정의하는데

ojc.asia

https://www.youtube.com/watch?v=Qj26gdH651A&list=PLxU-iZCqT52Cmj47aKB1T-SxI33YL7rYS&index=23 



https://www.youtube.com/watch?v=innYXvo9WpA&list=PLxU-iZCqT52Cmj47aKB1T-SxI33YL7rYS&index=2 

https://www.youtube.com/watch?v=vlVKwC1ALmM&list=PLxU-iZCqT52Cmj47aKB1T-SxI33YL7rYS&index=3 

https://www.youtube.com/watch?v=MGOb9QXi6So&list=PLxU-iZCqT52Cmj47aKB1T-SxI33YL7rYS&index=6 

WPF Style이란?

로컬 및 윈도우 스타일 적용





WPF Style 이란?

  • WPF XAML의 Resource절은 객체를 참조할 때도 사용되지만 Style 객체를 정의하는데도 이용된다. Style은 Element에 적용되는 스타일 프로퍼티의 집합이다.


  • XAML 안에서 프로그래밍 언어처럼 반복문을 사용할 수 없으므로 동일한 프로퍼티를 가지는 여러 요소들을 생성할 때 사용하면 좋다.


  • 페이지에 버튼이 많다고 가정할 때 Margin, Font 등의 속성은 비슷하게 이용되므로 Resource절 내에 Style로 정의하면 공용으로 사용할 수 있다. 이 방법이 스타일을 사용하는 일반적인 방법이다.


  • 웹페이지에 적용되는 Style과 비교했을 때 WPF의 Style은 다른 프로퍼티의 변화 또는 이벤트로부터 유발되는 프로퍼티의 변화를 제어할 수 있기에 더욱 강력하다.


  • Object를 상속받은 Style은 System.Window에 정의되어 있는데 중요한 프로퍼티는 Setter로 SetterBase 객체(Setter와 EventSetter가 상속받음)의 컬렉션인 SetterBaseCollection 타입의 프로퍼티로 이를 통해 프로퍼티나 이벤트 핸들러를 설정할 수 있다. 


  • Setter는 Style의 컨텐트 프로퍼티(ContentProperty)로 Setter와 EventSetter는 Style 요소의 자식이며 일반적으로 Setter가 더 많이 사용된다. EventSetter는 라우팅된 특정 이벤트의 이벤트핸들러를 설정하는데 사용된다.


  • Setter는 특정 프로퍼티와 값을 연결시키며 프로퍼티 타입(DependencyProperty 타입)과 Value 타입(object 타입) 2개의 프로퍼티가 있다.


<Style…>

<Setter Property=”Control.FontSize” Value=”12”/>

<EventSetter …/>

<Setter Property=”Control.FontSize” Value=”{x:Null}”/>

</Style>


  • 대부분 여러 개의 요소와 컨트롤들이 공유 가능하도록 Style을 Resource절에 사용하고 있으며 Application 객체의 Resource절에서 사용한 Style은 전체 응용프로그램에서 공용으로 사용 가능하다.



  • 스타일을 공유하지 않은 아래 경우를 살펴보자.
<Window x:Class="WpfApp2.MainWindow"
        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"
        xmlns:local="clr-namespace:WpfApp2"
        mc:Ignorable="d"
        Title="MainWindow" Height="250" Width="450">
    <Grid Height="175">
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Button x:Name="button" Content="Button2" HorizontalAlignment="Center" VerticalAlignment="Center" Width="100" Height="30">
            <Button.Style>
                <Style>
                    <Setter Property="Control.FontSize" Value="22" />
                    <Setter Property="Control.Foreground" Value="Red" />
                    <Setter Property="Control.HorizontalAlignment" Value="Center" />
                </Style>
            </Button.Style>
        </Button>
        <TextBlock x:Name="textBlock" HorizontalAlignment="Center" Grid.Row="1" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Center" Width="100" Height="30">
            <TextBlock.Style>
                <Style>
                    <Setter Property="Control.FontSize" Value="22" />
                    <Setter Property="Control.Foreground" Value="Red" />
                    <Setter Property="Control.HorizontalAlignment" Value="Center" />
                </Style>
            </TextBlock.Style>
        </TextBlock>
        <Button x:Name="button1" Content="Button2" Grid.Row="2" VerticalAlignment="Center" Width="100" Height="30" >
            <Button.Style>
                <Style>
                    <Setter Property="Control.FontSize" Value="22" />
                    <Setter Property="Control.Foreground" Value="Red" />
                    <Setter Property="Control.HorizontalAlignment" Value="Center" />
                </Style>
            </Button.Style>
        </Button>
    </Grid>
</Window>



  • 아래 예문은 Button과 TextBlock에서 Style을 공유했다.



<Window x:Class="WpfApp2.MainWindow"

        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"

        xmlns:local="clr-namespace:WpfApp2"

        mc:Ignorable="d"

        Title="MainWindow" Height="250" Width="450">

    <Window.Resources>

        <Style x:Key="normal">

            <Setter Property="Control.FontSize" Value="22" />

            <Setter Property="Control.Foreground" Value="Red" />

            <Setter Property="Control.HorizontalAlignment" Value="Center" />

        </Style>

    </Window.Resources>

    <Grid Height="175">

        <Grid.RowDefinitions>

            <RowDefinition/>

            <RowDefinition/>

            <RowDefinition/>

        </Grid.RowDefinitions>

        <Button Style="{StaticResource normal}" x:Name="button" Content="Button2" HorizontalAlignment="Center" VerticalAlignment="Center" Width="100" Height="30">

        </Button>

        <TextBlock Style="{StaticResource normal}"  x:Name="textBlock" HorizontalAlignment="Center" Grid.Row="1" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Center" Width="100" Height="30">

         </TextBlock>

        <Button Style="{StaticResource normal}"  x:Name="button1" Content="Button2" Grid.Row="2" VerticalAlignment="Center" Width="100" Height="30" >

        </Button>

    </Grid>

</Window>





#WPF동영상, #WPFStyle, #Resource, #StaticResource, #WPF스타일, #WPF교육, #WPF강좌, WPF동영상, WPFStyle, Resource, StaticResource, WPF스타일, 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...