레이블이 #BasedOn인 게시물을 표시합니다. 모든 게시물 표시
레이블이 #BasedOn인 게시물을 표시합니다. 모든 게시물 표시

2019년 1월 26일 토요일

WPF 스타일(Style), BasedOn, TargetType, 스타일상속

WPF 스타일(Style), BasedOn, TargetType, 스타일상속
이미 만들어진 Style을 기반으로 약간의 프로퍼티 정의를 추가한 Style을 정의하는 것도 가능한데 이 경우 Style에서 기존 Style을 참조하기 위해BasedOn 속성을 사용할 수 있다.
<Window x:Class="WpfApp6.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:WpfApp6"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<StackPanel.Resources>
<Style x:Key="norbtn">
<Setter Property="Control.FontSize" Value="24" />
<Setter Property="Control.Foreground" Value="Blue" />
<Setter Property="Control.HorizontalAlignment" Value="Center" />
<Setter Property="Control.Margin" Value="24" />
<Setter Property="Control.Padding" Value="20, 10, 20, 10" />
</Style>
<Style x:Key="hotbtn" BasedOn="{StaticResource norbtn}">
<Setter Property="Control.Foreground" Value="Red" />
</Style>
</StackPanel.Resources>
<Button Style="{StaticResource norbtn}">
Button Number 1
</Button>
<Button Style="{StaticResource hotbtn}">
Button Number 2
</Button>
<Button Style="{StaticResource norbtn}">
Button Number 3
</Button>
</StackPanel>
</Window>
TargetType 속성을 가진 기존 스타일을 기본으로해서 정의하는 것도 가능하지만 조금 복잡해 진다.
BasedOn=”{StaticResource {x:Type Button}}”
새로 정의된 Style에서는 스타일이 바탕으로 한 클래스나 그 클래스의 파생클래스로 TargetType을 설정해야 한다.
<Window x:Class="WpfApp6.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:WpfApp6"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<StackPanel.Resources>
<Style TargetType="{x:Type Button}">
<Setter Property="Control.FontSize" Value="24" />
<Setter Property="Control.Foreground" Value="Blue" />
<Setter Property="Control.HorizontalAlignment" Value="Center" />
<Setter Property="Control.Margin" Value="24" />
</Style>
ß 두번째 정의하는 스타일은 충돌을 피하기 위해 x:Key 필요 à
<Style x:Key="hotbtn"
TargetType="{x:Type Button}"
BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="Control.Foreground" Value="Red" />
</Style>
</StackPanel.Resources>
<Button>
Button Number 1
</Button>
<Button Style="{StaticResource hotbtn}">
Button Number 2
</Button>
<Button>
Button Number 3
</Button>
</StackPanel>
</Window>
TargetType을 사용하면 특정 타입 엘리먼트는 항상 특정 스타일을 가지며 클래스의 계층과 일치하는 스타일의 계층을 다음과 같이 정의할 수 있다.
<Window x:Class="WpfApp6.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:WpfApp6"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<StackPanel.Resources>
<Style TargetType="{x:Type Control}">
<Setter Property="Control.FontSize" Value="24" />
<Setter Property="Control.Foreground" Value="Blue" />
<Setter Property="Control.HorizontalAlignment" Value="Center" />
<Setter Property="Control.Margin" Value="24" />
</Style>
<Style TargetType="{x:Type Button}"
BasedOn="{StaticResource {x:Type Control}}">
<Setter Property="Control.Foreground" Value="Red" />
</Style>
<Style TargetType="{x:Type Label}"
BasedOn="{StaticResource {x:Type Control}}">
<Setter Property="Control.Foreground" Value="Green" />
</Style>
<Style TargetType="{x:Type TextBox}"
BasedOn="{StaticResource {x:Type Control}}">
</Style>
</StackPanel.Resources>
<Button>
Button Control
</Button>
<Label>
Label Control
</Label>
<TextBox>
TextBox Control
</TextBox>
</StackPanel>
</Window>

(C#교육동영상)C# ADO.NET 실습 ODP.NET/ODAC 설치 오라클 함수 호출 실습, C#학원, WPF학원, 닷넷학원, 자바학원

  (C#교육동영상)C# ADO.NET 실습  ODP.NET/ODAC 설치  오라클 함수 호출 실습, C#학원, WPF학원, 닷넷학원, 자바학원 https://www.youtube.com/watch?v=qIPU85yAlzc&list=PLxU-i...