■ EventTrigger 엘리먼트 사용하기

----------------------------------------------------------------------------------------------------

Product.cs

 

 

namespace TestProject

{

    /// <summary>

    /// 제품

    /// </summary>

    public class Product

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 명칭 - Name

 

        /// <summary>

        /// 명칭

        /// </summary>

        public string Name { get; set; }

 

        #endregion

 

        #region 가격 - Price

 

        /// <summary>

        /// 가격

        /// </summary>

        public string Price { get; set; }

 

        #endregion

    }

}

 

 

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"

    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

    Width="600"

    Height="450"

    Title="메인 윈도우">

    <Grid>

        <ListBox Name="listBox"

            ItemsSource="{Binding ProductList}">

            <ListBox.ItemTemplate>

                <DataTemplate>

                    <Border

                        BorderThickness="1"

                        BorderBrush="Gold">

                        <i:Interaction.Triggers>

                            <i:EventTrigger EventName="MouseLeftButtonDown">

                                <i:InvokeCommandAction

                                    Command="{Binding RelativeSource={RelativeSource AncestorType=Window,

                                        Mode=FindAncestor}, Path=DataContext.OnMouseLeftButtonDownCommand}" />

                            </i:EventTrigger>

                        </i:Interaction.Triggers>

                        <StackPanel Orientation="Horizontal">

                            <TextBlock Text="{Binding Path=Name}" />

                            <TextBlock Text="{Binding Path=Price}" />

                        </StackPanel>

                    </Border>

                </DataTemplate>

            </ListBox.ItemTemplate>

        </ListBox>

    </Grid>

</Window>

 

 

MainWindow.xaml.cs

 

 

using System.Windows;

using System.Collections.ObjectModel;

using System.Windows.Controls;

using System.Windows.Input;

 

namespace TestProject

{

    /// <summary>

    /// 메인 윈도우

    /// </summary>

    public partial class MainWindow : Window

    {

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainWindow()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainWindow()

        {

            InitializeComponent();

 

            DataContext = new MainWindowViewModel();

        }

 

        #endregion

    }

}

 

 

MainWindowViewModel.cs

 

 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Windows;

 

namespace TestProject

{

    /// <summary>

    /// 메인 윈도우 뷰 모델

    /// </summary>

    public class  MainWindowViewModel : INotifyPropertyChanged

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Event

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 속성 변경시 - PropertyChanged

 

        /// <summary>

        /// 속성 변경시

        /// </summary>

        public event PropertyChangedEventHandler PropertyChanged;

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 제품 리스트 - ProductList

 

        /// <summary>

        /// 제품 리스트

        /// </summary>

        public List<Product> ProductList

        {

            get

            {

                return new List<Product>()

                {

                    new Product(){Name = "Product1",Price = "Price1"},

                    new Product(){Name = "Product2",Price = "Price2"}

                };

            }

        }

 

        #endregion

 

        #region 마우스 왼쪽 버튼 DOWN 명령 - OnMouseLeftButtonDownCommand

 

        /// <summary>

        /// 마우스 왼쪽 버튼 DOWN 명령

        /// </summary>

        public RelayCommand OnMouseLeftButtonDownCommand { get; set; }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainWindowViewModel()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainWindowViewModel()

        {

            OnMouseLeftButtonDownCommand = new RelayCommand((n)=> MessageBox.Show("MouseLeftButtonDown 이벤트가 발생했습니다."));

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Protected

 

        #region 속성 변경시 처리하기 - OnPropertyChanged(propertyName)

 

        /// <summary>

        /// 속성 변경시 처리하기

        /// </summary>

        /// <param name="propertyName">속성명</param>

        protected void OnPropertyChanged(string propertyName)

        {

            if(PropertyChanged != null)

            {

                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));

            }

        }

 

        #endregion

    }

}

 

----------------------------------------------------------------------------------------------------

※ RelayCommand 클래스 : "MVVM 패턴 사용하기" 참조

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요