WPF 기본동작
Visual Studio를 이용하여 WPF 응용프로그램을 새로 만들면, App.xaml 과 MainWindow.xaml 이렇게 두개가 생성이 된다.
우선 App.xaml은 WPF application의 entry point 이다.
App.xaml
<Application x:Class="WPFApplication.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>
Xaml 과 코드를 연결해 사용하기 위해 x:Class attribute 를 사용한다.
Attribute는 namespaceName.className 형태로 할당하고, 해당 클래스는 반드시 Partial class로 선ㅇ너되어야 한다.
xmlns:x
System.Windows.Markup 을 참조한다는 의미로 Xaml 언어 기능 자체를 지원하기 위한 namespace 이다.
x:Class
Xaml 의 루트 요소 즉 Window, Page, Application 에 code behind 를 정희하기 위해 사용하며
code behind 에 정의된 class는 반드시 partial class로 선언되어야 한다.
x:Key
ResourceDictionary와 같이 IDictionary를 구현한 부모에 자식을 추가할 때는 고유한 식별자 값을 제공해야 하는데
x:Key 를 통해 가능하다.
<window.Resource>
<Style x:Key="MyStyle">
<Setter Property="control.Background" Value="Yellow" />
</Style>
</window.Resource>
하지만 TragetType을 직접 선언하는 경우 x:Key 값으로 식별하지 않아도 된다.
예와 같은 경우 범위 안에 있는 모든 TextBlock에 적용하겠다는 의미가 된다.
<window.Resource>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="FontSize" Value="12pt" />
</Style>
</window.Resource>
x:Name 은 인스턴스화된 객체에 접근하기 위해 사용하며, x:Key 와 같이 사용할 수가 없다.
x:Null
객체의 Property를 명시적으로 null로 선언하기 위해 사용한다.
<object Property="{x:Null}">
</object>
x:Static
상수(Const), 정적 속성(Static value), 필드(field), 열거형(enumeration) 값을 사용할 때 사용하는 태그 확장이다.
다른값을 x:Static 에 사용했을 때는 오류가 발생한다.
<object Property="{x:Static prefix:typeName.staticMemberName}">
</object>
열거형(enumeration) 값을 사용할 때
namespace my
{
public enum ChartGrade
{
gradeA,
gradeB,
gradeC
}
}
<object Property="{x:Static my:ChartGrade.gradeA}">
</object>
x:TypeAgruments
제네릭 형식 클래스에 Type을 전달하기 위해 사용한다.
x:TypeArguments 는 x:Class 와 같이 사용되기 때문에 Window 나 Page 등과 사용되어야 한다.
... x:TypeAgruments="sys:String" ...