■ ListBox 엘리먼트 : ItemContainerStyle 속성을 사용해 이벤트 설정하기

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

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

    Height="450"

    Title="메인 윈도우">

    <Grid>

        <ListBox Name="listBox">

            <ListBox.ItemTemplate>

                <DataTemplate>

                    <Button Name="TaskButton"

                        Margin="5"

                        Width="100"

                        Height="30"

                        Content="{Binding}" />

                </DataTemplate>

            </ListBox.ItemTemplate>

            <ListBox.ItemContainerStyle>

                <Style TargetType="{x:Type ListBoxItem}">

                    <EventSetter

                        Event="PreviewMouseDown"

                        Handler="listBoxItem_PreviewMouseDown" />

                </Style>

            </ListBox.ItemContainerStyle>

        </ListBox>

    </Grid>

</Window>

 

 

MainWindow.xaml.cs

 

 

using System.Windows;

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();

 

            this.listBox.ItemsSource = new string[] { "A", "B", "C", "D", "E" };

        }

 

        #endregion

 

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

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region 리스트 박스 항목 마우스 DOWN 미리보기 처리하기 - listBoxItem_PreviewMouseDown(sender, e)

 

        /// <summary>

        /// 리스트 박스 항목 마우스 DOWN 미리보기 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void listBoxItem_PreviewMouseDown(object sender, MouseButtonEventArgs e)

        {

            ((ListBoxItem) sender).IsSelected = true;

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요