본문으로 바로가기

Trigger, DataTrigger & EventTrigger

category Development/C# 2016.03.24 16:02

Property trigger

<Window x:Class="WpfTutorialSamples.Styles.StyleTriggersSample"
       
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
       
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       
Title="StyleTriggersSample" Height="100" Width="300">
   
<Grid>
       
<TextBlock Text="Hello, styled world!" FontSize="28" HorizontalAlignment="Center" VerticalAlignment="Center">
           
<TextBlock.Style>
               
<Style TargetType="TextBlock">
                   
<Setter Property="Foreground" Value="Blue"></Setter>
                   
<Style.Triggers>
                       
<Trigger Property="IsMouseOver" Value="True">
                           
<Setter Property="Foreground" Value="Red" />
                           
<Setter Property="TextDecorations" Value="Underline" />
                       
</Trigger>
                   
</Style.Triggers>
               
</Style>
           
</TextBlock.Style>
       
</TextBlock>
   
</Grid>
</Window>

 

Data trigger

<Window x:Class="WpfTutorialSamples.Styles.StyleDataTriggerSample"
       
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
       
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       
Title="StyleDataTriggerSample" Height="200" Width="200">
   
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
       
<CheckBox Name="cbSample" Content="Hello, world?" />
       
<TextBlock HorizontalAlignment="Center" Margin="0,20,0,0" FontSize="48">
           
<TextBlock.Style>
               
<Style TargetType="TextBlock">
                   
<Setter Property="Text" Value="No" />
                   
<Setter Property="Foreground" Value="Red" />
                   
<Style.Triggers>
                       
<DataTrigger Binding="{Binding ElementName=cbSample, Path=IsChecked}" Value="True">
                           
<Setter Property="Text" Value="Yes!" />
                           
<Setter Property="Foreground" Value="Green" />
                       
</DataTrigger>
                   
</Style.Triggers>
               
</Style>
           
</TextBlock.Style>
       
</TextBlock>
   
</StackPanel>
</Window>

 

Event trigger

<Window x:Class="WpfTutorialSamples.Styles.StyleEventTriggerSample"
       
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
       
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       
Title="StyleEventTriggerSample" Height="100" Width="300">
   
<Grid>
       
<TextBlock Name="lblStyled" Text="Hello, styled world!" FontSize="18" HorizontalAlignment="Center" VerticalAlignment="Center">
           
<TextBlock.Style>
               
<Style TargetType="TextBlock">
                   
<Style.Triggers>
                       
<EventTrigger RoutedEvent="MouseEnter">
                           
<EventTrigger.Actions>
                               
<BeginStoryboard>
                                   
<Storyboard>
                                       
<DoubleAnimation Duration="0:0:0.300" Storyboard.TargetProperty="FontSize" To="28" />
                                   
</Storyboard>
                               
</BeginStoryboard>
                           
</EventTrigger.Actions>
                       
</EventTrigger>
                       
<EventTrigger RoutedEvent="MouseLeave">
                           
<EventTrigger.Actions>
                               
<BeginStoryboard>
                                   
<Storyboard>
                                       
<DoubleAnimation Duration="0:0:0.800" Storyboard.TargetProperty="FontSize" To="18" />
                                   
</Storyboard>
                               
</BeginStoryboard>
                           
</EventTrigger.Actions>
                       
</EventTrigger>
                   
</Style.Triggers>
               
</Style>
           
</TextBlock.Style>
       
</TextBlock>
   
</Grid>
</Window>

'Development > C#' 카테고리의 다른 글

Trigger, DataTrigger & EventTrigger  (0) 2016.03.24
MwnuItem Command MVVM 구현 방법  (0) 2016.03.21
iis설정 정보 파일  (0) 2016.03.17
SQLServer DataType vs .NET(C#) DataType  (0) 2016.03.16
Style BaseOn  (0) 2016.03.11
UI Thread Binding 처리  (0) 2016.03.08

댓글을 달아 주세요