728x90
반응형
728x170
▶ MainWindow.xaml
<Window x:Class="TestProject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="800"
Height="600"
Title="EventTrigger 클래스 : 코드로 이벤트 트리거 사용하기"
FontFamily="나눔고딕코딩"
FontSize="16">
<Window.Resources>
<Style x:Key="ImageStyleKey" TargetType="{x:Type Image}">
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="Width" Value="100" />
<Setter Property="Height" Value="100" />
<Setter Property="Margin" Value="10" />
</Style>
</Window.Resources>
<StackPanel Name="stackPanel"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Orientation="Horizontal" />
</Window>
728x90
▶ MainWindow.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
namespace TestProject
{
/// <summary>
/// 메인 윈도우
/// </summary>
public partial class MainWindow : Window
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary>
/// 이미지 파일명 배열
/// </summary>
private string[] imageFileNameArray = { "겨울.jpg", "석양.jpg", "수련.jpg", "푸른언덕.jpg" };
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - MainWindow()
/// <summary>
/// 생성자
/// </summary>
public MainWindow()
{
InitializeComponent();
Loaded += Window_Loaded;
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Private
#region 윈도우 로드시 처리하기 - Window_Loaded(sender, e)
/// <summary>
/// 윈도우 로드시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void Window_Loaded(object sender, RoutedEventArgs e)
{
#region 마우스 진입시 이벤트 트리거를 설정한다.
DoubleAnimation widthAnimation1 = new DoubleAnimation(200, new Duration(TimeSpan.Parse("0:0:0.15")));
DoubleAnimation heightAnimation1 = new DoubleAnimation(200, new Duration(TimeSpan.Parse("0:0:0.15")));
Storyboard.SetTargetProperty(widthAnimation1 , new PropertyPath(WidthProperty ));
Storyboard.SetTargetProperty(heightAnimation1, new PropertyPath(HeightProperty));
Storyboard storyboard1 = new Storyboard();
storyboard1.Children.Add(widthAnimation1 );
storyboard1.Children.Add(heightAnimation1);
BeginStoryboard beginStoryboard1 = new BeginStoryboard();
beginStoryboard1.Storyboard = storyboard1;
EventTrigger eventTrigger1 = new EventTrigger(MouseEnterEvent);
eventTrigger1.Actions.Add(beginStoryboard1);
#endregion
#region 마우스 이탈시 이벤트 트리거를 설정한다.
DoubleAnimation widthAnimation2 = new DoubleAnimation(100, new Duration(TimeSpan.Parse("0:0:0.15")));
DoubleAnimation heightAnimation2 = new DoubleAnimation(100, new Duration(TimeSpan.Parse("0:0:0.15")));
Storyboard.SetTargetProperty(widthAnimation2 , new PropertyPath(WidthProperty ));
Storyboard.SetTargetProperty(heightAnimation2, new PropertyPath(HeightProperty));
Storyboard storyboard2 = new Storyboard();
storyboard2.Children.Add(widthAnimation2 );
storyboard2.Children.Add(heightAnimation2);
BeginStoryboard beginStoryboard2 = new BeginStoryboard();
beginStoryboard2.Storyboard = storyboard2;
EventTrigger eventTrigger2 = new EventTrigger(MouseLeaveEvent);
eventTrigger2.Actions.Add(beginStoryboard2);
#endregion
foreach(string imageFileName in imageFileNameArray)
{
Image image = new Image();
image.Style = (Style)this.Resources["ImageStyleKey"];
image.Stretch = Stretch.Fill;
image.Source = new BitmapImage(new Uri($"pack://application:,,,/IMAGE/{imageFileName}"));
image.Triggers.Add(eventTrigger1);
image.Triggers.Add(eventTrigger2);
this.stackPanel.Children.Add(image);
}
}
#endregion
}
}
728x90
반응형
그리드형(광고전용)
'C# > WPF' 카테고리의 다른 글
[C#/WPF] x:Array 엘리먼트 : 배열 사용하기 (0) | 2021.02.21 |
---|---|
[C#/WPF] x:Reference 태그 확장 사용하기 (0) | 2021.02.21 |
[C#/WPF] x:Shared 속성 사용하기 (0) | 2021.02.20 |
[C#/WPF] Shape 클래스 : 환형 진행바 사용하기 (0) | 2021.02.20 |
[C#/WPF] 마우스를 사용해 엘리먼트 이동/회전/확대/축소하기 (0) | 2021.02.20 |
[C#/WPF] EventTrigger 클래스 : 코드로 이벤트 트리거 사용하기 (0) | 2021.02.20 |
[C#/WPF] Image 클래스 : 마우스를 사용해 이미지 회전시키기 (0) | 2021.02.20 |
[C#/WPF] ComponentDispatcher 클래스 : ThreadFilterMessage 정적 이벤트를 사용해 프로세스 간 메시지 송수신하기 (0) | 2021.02.20 |
[C#/WPF] Slider 클래스 : 커스텀 슬라이더 사용하기 (0) | 2021.02.20 |
[C#/WPF] Window 클래스 : 반투명 윈도우 사용하기 (0) | 2021.02.19 |
[C#/WPF] MediaElement 클래스 : 움직이는 GIF 파일 재생하기 (0) | 2021.02.19 |
댓글을 달아 주세요