템플릿(Template) : 컨트롤 모양 미리 정의
.NET프로그래밍/Silverlight 3.0 2009. 12. 3. 10:23 |[FrmControlTemplate.xaml]
<UserControl x:Class="RiaTemplate.FrmControlTemplate"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
<!--버튼의 모양을 직접 정의-->
<Button Width="200" Height="100">
<Button.Template>
<ControlTemplate TargetType="Button">
<Border BorderBrush="Blue" BorderThickness="3">
<TextBlock Text="템플릿이 적용된 버튼"
HorizontalAlignment="Center" VerticalAlignment="Center" />
</Border>
</ControlTemplate>
</Button.Template>
</Button>
</Grid>
</UserControl>
[FrmTemplateByStyle.xaml]
<UserControl x:Class="RiaTemplate.FrmTemplateByStyle"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
<Grid.Resources>
<!-- 스타일로 컨트롤 템플릿 구현 -->
<Style x:Key="myButton" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border BorderBrush="Red" BorderThickness="3" Background="Yellow">
<TextBlock Text="템플릿이 적용된 버튼"
HorizontalAlignment="Center" VerticalAlignment="Center" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
<StackPanel>
<Button Width="200" Height="50" Content="버튼1" Style="{StaticResource myButton}" />
<Button Width="200" Height="50" Content="버튼2" Style="{StaticResource myButton}" />
<Button Width="200" Height="50" Content="버튼3" />
</StackPanel>
</Grid>
</UserControl>
[FrmTemplateBinding.xaml]
<UserControl x:Class="RiaTemplate.FrmTemplateBinding"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
<Grid.Resources>
<!-- 스타일로 컨트롤 템플릿 구현 -->
<Style x:Key="myButton" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border BorderBrush="Red" BorderThickness="3" Background="Yellow">
<TextBlock Text="{TemplateBinding Content}"
HorizontalAlignment="Center" VerticalAlignment="Center" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
<StackPanel>
<Button Width="200" Height="50" Content="버튼1" Style="{StaticResource myButton}" />
<Button Width="200" Height="50" Style="{StaticResource myButton}">
<Button.Content>
<TextBlock Text="버튼2"></TextBlock>
</Button.Content>
</Button>
<Button Width="200" Height="50" Content="버튼3" Style="{StaticResource myButton}" />
</StackPanel>
</Grid>
</UserControl>
[FrmContentPresenter.xaml]
<UserControl x:Class="RiaTemplate.FrmContentPresenter"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
<Grid.Resources>
<!-- 스타일로 컨트롤 템플릿 구현 -->
<Style x:Key="myButton" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border BorderBrush="Red" BorderThickness="3" Background="Yellow">
<!--
<TextBlock Text="{TemplateBinding Content}"
HorizontalAlignment="Center" VerticalAlignment="Center" />
-->
<!-- ContentPresenter : 텍스트를 포함한 요소 자체를 표현 -->
<ContentPresenter />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
<StackPanel>
<Button Width="200" Height="50" Content="버튼1" Style="{StaticResource myButton}" />
<Button Width="200" Height="50" Style="{StaticResource myButton}">
<Button.Content>
<TextBlock Text="버튼2" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
</Button.Content>
</Button>
<Button Width="200" Height="50" Content="버튼3" Style="{StaticResource myButton}" />
</StackPanel>
</Grid>
</UserControl>
'.NET프로그래밍 > Silverlight 3.0' 카테고리의 다른 글
<DoubleAnimation /> - 이미지에 Animation 효과 주기(FadeIn) (0) | 2009.12.03 |
---|---|
애니메이션 (Animation) (0) | 2009.12.03 |
스타일(Style) : 재 사용 가능한 속성 집합 (0) | 2009.12.03 |
리소스 (Resource) (0) | 2009.12.03 |
DatePicker 컨트롤 (0) | 2009.12.02 |