yoshikazuendo
1/20/2015 - 3:18 AM

【WPF】単一選択のListBoxで選択解除の機能をBehaviorで実装してみた。 ※参考サイト ・stakcoverflow http://stackoverflow.com/questions/5139956/listbox-with-single-select-and-a

【WPF】単一選択のListBoxで選択解除の機能をBehaviorで実装してみた。 ※参考サイト ・stakcoverflow http://stackoverflow.com/questions/5139956/listbox-with-single-select-and-also-unselect-on-click ・WPFビヘイビアなるもの https://tocsworld.wordpress.com/2014/07/02/wpf-%E3%83%93%E3%83%98%E3%82%A4%E3%83%93%E3%82%A2-%E3%81%AA%E3%82%8B%E3%82%82%E3%81%AE/

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interactivity;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ListBoxTest
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            for (int i = 0; i < 10; i++)
            {
                this.Text.Items.Add("hogehoge");
                this.Text.Items.Add("hugehuge");
                this.Text.Items.Add("aikazuyendo");
            }
        }
    }
}

namespace TestAikazu
{
    /// <summary>
    /// ListBox単一選択の選択・選択解除を提供するBehaviorです。
    /// </summary>
    public class ListBoxSelectionBehavior : Behavior<ListBox>
    {
        /// <summary>
        /// イベントを登録します。
        /// BehaviorがListBoxにアタッチされた後で呼び出されます。
        /// </summary>
        protected override void OnAttached()
        {
            base.OnAttached();

            if (AssociatedObject == null)
            {
                return;
            }

            // イベント登録
            AssociatedObject.SelectionMode = SelectionMode.Multiple;
            AssociatedObject.SelectionChanged += OnSelectionChanged;
        }

        /// <summary>
        /// BehaviorがListBoxにでタッチされる時に呼び出せます。
        /// </summary>
        protected override void OnDetaching()
        {
            if (AssociatedObject != null)
            {
                // イベント削除
                AssociatedObject.SelectionChanged -= OnSelectionChanged;
                AssociatedObject.SelectionMode = SelectionMode.Single;
            }

            base.OnDetaching();
        }

        /// <summary>
        /// SelectionChangedイベント処理
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        protected virtual void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                var target = e.AddedItems[0];
                var listBox = sender as ListBox;

                if (listBox != null)
                {
                    foreach (var item in new ArrayList(listBox.SelectedItems))
                    {
                        // 選択済みのアイテムがクリックされた場合は選択解除する。
                        if (item.GetHashCode() != target.GetHashCode())
                        {
                            listBox.SelectedItems.Remove(item);
                        }
                    }
                }
            }
        }
    }
}
<Window x:Class="ListBoxTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        xmlns:my ="clr-namespace:TestAikazu"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ListBox x:Name="Text">
            <i:Interaction.Behaviors>
                <my:ListBoxSelectionBehavior/>
            </i:Interaction.Behaviors>
        </ListBox>
    </Grid>
</Window>