XAML의 Resource절은 객체를 참조할 때도 사용되지만 Style 객체를 정의하는데도 이용된다. Style은 Element에 적용되는 스타일 프로퍼티의 집합이다.
XAML 안에서 프로그래밍 언어처럼 반복문을 사용할 수 없으므로 동일한 프로퍼티를 가지는 여러 요소들을 생성할 때 사용하면 좋다.
페이지에 버튼이 많다고 가정할 떄 Margin,Font 등의 속성은 비슷하게 이용되므로 Resource절 내에 style로 정의하면 공용으로 사용할 수 있다.
웹페이지에 적용되는 style가 비교했을 때 WPF의 style은 다른 프로퍼티의 변화 또는 이벤트로부터 유발되는 프로퍼티의 변화를 제어할 수 있기에 더욱 강력하다.
object를 상속받은 style은 System.Window에 정의되어 있으며 중요한 프로퍼티는 Setter로 SetterBase 객체(Setter와 EventSetter가 상속받음)의 컬렉션인 SetterBaseCollection 타입의 프로퍼티로 이를 통해 프로퍼티나 이벤트 핸들러를 설정할 수 있다.
Setter는 style의 컨텐트프로퍼티로 Setter와 EventSetter는 style요소의 자식이며 일반적으로 setter가 더 많이 사용된다.
setter는 특정 프로퍼티와 값을 연결시키며 프로퍼티 타입과 value타입 2개의 프로퍼티가 있다.
<style>
<Stter Property="Control.FontSize" Value="12"/>
<EventSetter.../>
<Setter Property="Control.FontSize" Value="{x:Null}"/>
</style>
<Button Horizontallignment="Center" VerticalAlignment="Center" Foreground="Blue">
<Button.Style>
<Style>
<Setter Property="Button.FontSize" Value="16pt"/>
<Setter Property="Button.FontWeight" Value="Bold"/>
<Setter Property="Button.FontFamily" Value="Constantia"/>
<Setter Property="Button.Fontground" Value="Red"/>
</Style>
</Button.Style>
Button Test
</Button>