(WPF동영상)WPF Style이란? Resource, StaticResource 이용 WPF 스타일 적용, WPF교육, C#교육, WPF학원, C#학원
http://ojc.asia/bbs/board.php?bo_table=WPF&wr_id=185
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강좌,