첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
0.28MB

▶ 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
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요