[WPF] Resource WPF 2012. 3. 14. 17:42

리소스는 런타임 당시 이용되어 쓰이는 데이터를 말하는 것이다. 아래는 리소스를 이용하여 컨트롤 변경 하는 예제이다.

앞서 포스팅 한 글에도 Window.Resource 를 정의하여 이용 했는데 그곳에 정의 된 내용을 쓰는 것과 동일한 내용이다.

<Window.Resources>
        <Style x:Key="LabelStyle1" TargetType="{x:Type Label}">
            <Setter Property="Foreground" Value="#FFFF0000"/>
            <Setter Property="Background" Value="#00FF0000"/>
            <Setter Property="Padding" Value="5"/>
            <Setter Property="HorizontalContentAlignment" Value="Left"/>
            <Setter Property="VerticalContentAlignment" Value="Top"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Label}">
                        <Border SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
                            <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" RecognizesAccessKey="True"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="FontWeight" Value="Bold"/>
        </Style>
        <Style x:Key="LabelStyle2" TargetType="{x:Type Label}">
            <Setter Property="Foreground" Value="#FF002CFF"/>
            <Setter Property="Background" Value="#00FF0000"/>
            <Setter Property="Padding" Value="5"/>
            <Setter Property="HorizontalContentAlignment" Value="Left"/>
            <Setter Property="VerticalContentAlignment" Value="Top"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Label}">
                        <Border SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
                            <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" RecognizesAccessKey="True"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="FontWeight" Value="Normal"/>
            <Setter Property="FontStyle" Value="Italic"/>
            <Setter Property="FontFamily" Value="Gulim"/>
            <Setter Property="FontSize" Value="20"/>
        </Style>

    </Window.Resources>

위와 같이 리소스를 정의 하고,

<Label Height="28" Margin="14,33,12,0" Name="label1" VerticalAlignment="Top" Padding="0"
               Style="{DynamicResource null}">Test</Label>
        <Button Height="23" HorizontalAlignment="Left" Margin="14,0,0,46" Name="btn1" VerticalAlignment="Bottom"  Width="75" Click="btn1_Click">Button Test</Button>
        <Button Height="23" Margin="105,0,98,46" Name="btn2" VerticalAlignment="Bottom"  Width="75" Click="btn2_Click">Resource1</Button>
        <Button Height="23" HorizontalAlignment="Right" Margin="0,0,12,46" Name="btn3" VerticalAlignment="Bottom"  Width="75" Click="btn3_Click">Resource2</Button>

와 같이 컨트롤을 올린 후 이벤트에 style을 FindResource를 이용하여 적용 시키면 된다. 명시적으로 캐스팅 해 주는 것을 잊지 말자.