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

■ ListBox 클래스 : SelectionChanged 이벤트 사용하기

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


TestProject.zip



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:local="clr-namespace:TestProject"

    Width="800"

    Height="600"

    Title="ListBox 클래스 : SelectionChanged 이벤트 사용하기"

    FontFamily="나눔고딕코딩"

    FontSize="16">

    <Window.Resources>

        <local:ColorCollection x:Key="ColorCollectionKey" />

    </Window.Resources>

    <Grid>

        <StackPanel

            HorizontalAlignment="Center"

            VerticalAlignment="Center">

            <WrapPanel Name="wrapPanel"

                Orientation="Horizontal">

                <WrapPanel.Resources>

                    <Style TargetType="Rectangle">

                        <Setter Property="Margin" Value="10" />

                        <Setter Property="Height" Value="20" />

                        <Setter Property="Width" Value="20" />

                    </Style>

                </WrapPanel.Resources>

            </WrapPanel>

            <ListBox Name="listBox"

                HorizontalAlignment="Center"

                Width="200"

                Height="150"

                Background="HoneyDew"

                SelectionMode="Extended"

                ItemsSource="{Binding Source={StaticResource ColorCollectionKey}}" />

        </StackPanel>

    </Grid>

</Window>

 

 

MainWindow.xaml.cs

 

 

using System.Windows;

using System.Windows.Controls;

using System.Windows.Media;

using System.Windows.Shapes;

 

namespace TestProject

{

    /// <summary>

    /// 메인 윈도우

    /// </summary>

    public partial class MainWindow : Window

    {

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

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

 

        #region 생성자 - MainWindow()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainWindow()

        {

            InitializeComponent();

 

            this.listBox.SelectionChanged += listBox_SelectionChanged;

        }

 

        #endregion

 

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

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

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

 

        #region 리스트 박스 선택 변경시 처리하기 - listBox_SelectionChanged(sender, e)

 

        /// <summary>

        /// 리스트 박스 선택 변경시 처리하기

        /// </summary>

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

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

        private void listBox_SelectionChanged(object sender, SelectionChangedEventArgs e)

        {

            BrushConverter converter = new BrushConverter();

 

            foreach(string color in e.AddedItems)

            {

                if(GetRectangle(color) == null)

                {

                    Rectangle rectangle = new Rectangle();

 

                    rectangle.Fill = (Brush)converter.ConvertFrom(color);

                    rectangle.Tag  = color;

 

                    this.wrapPanel.Children.Add(rectangle);

                }

            }

 

            foreach(string color in e.RemovedItems)

            {

                Rectangle rectangle = GetRectangle(color);

 

                if(rectangle != null)

                {

                    this.wrapPanel.Children.Remove(rectangle);

                }

            }

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////// Function

 

        #region 사각형 구하기 - GetRectangle(color)

 

        /// <summary>

        /// 사각형 구하기

        /// </summary>

        /// <param name="color">색상</param>

        /// <returns>사각형</returns>

        private Rectangle GetRectangle(string color)

        {

            foreach(FrameworkElement child in this.wrapPanel.Children)

            {

                if(child is Rectangle rectangle)

                {

                    if(rectangle.Tag.ToString() == color)

                    {

                        return rectangle;

                    }

                }

            }

 

            return null;

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker
TAG , ,

댓글을 달아 주세요