첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

728x90
반응형

■ EventTrigger 엘리먼트 : 마우스 왼쪽 버튼 DOWN 처리하기

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


TestProject.zip


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="800"

    Height="600"

    Title="EventTrigger 엘리먼트 : 마우스 왼쪽 버튼 DOWN 처리하기"

    FontFamily="나눔고딕코딩"

    FontSize="16">

    <Grid>

        <ListBox Name="listBox"

            ItemsSource="{Binding ProductList}">

            <ListBox.ItemTemplate>

                <DataTemplate>

                    <Border

                        Margin="10"

                        BorderThickness="1"

                        BorderBrush="Gold"

                        Padding="10">

                        <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=" : " />

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

                        </StackPanel>

                    </Border>

                </DataTemplate>

            </ListBox.ItemTemplate>

        </ListBox>

    </Grid>

</Window>

 

 

MainWindow.xaml.cs

 

 

using System.Windows;

 

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.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

    }

}

 

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

728x90
반응형
Posted by 사용자 icodebroker
TAG , ,

댓글을 달아 주세요