using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows;

namespace Txgy.Controls
{

    public class myListBox : System.Windows.Controls.ListBox
    {
        protected override DependencyObject GetContainerForItemOverride()
        {
            return new myListBoxItem();
        }
        //protected override void OnSelectionChanged(SelectionChangedEventArgs e)
        //{
        //    //base.OnSelectionChanged(e);

        //    if (this.SelectedItem == null && _lastSelectedItem != null)
        //    {
        //        // Restore the last selected item if nothing is selected
        //        _lastSelectedItem.IsSelected = true;
        //    }
        //    else
        //    {
        //        // Update the last selected item
        //        _lastSelectedItem = this.SelectedItem as ListBoxItem;
        //    }
        //}
    }
    public class myListBoxItem : System.Windows.Controls.ListBoxItem
    {
        protected override void OnSelected(System.Windows.RoutedEventArgs e)
        {
            DependencyObject dep = (DependencyObject)e.OriginalSource;

            while ((dep != null) && !(dep is ListBoxItem))
            {
                dep = VisualTreeHelper.GetParent(dep);
            }

            if (dep == null)
                return;

            //ListBoxItem item = (ListBoxItem)dep;

            //if (item.IsSelected)
            //{
            //    item.IsSelected = !item.IsSelected;
            //    //e.Handled = true;
            //}
            base.OnSelected(e);
        }
        //protected override void OnMouseLeave(System.Windows.Input.MouseEventArgs e)
        //{
        //    // 防止离开后状态改变
        //    if (IsSelected)
        //    {
        //        IsSelected = true;
        //    }

        //    base.OnMouseLeave(e);
        //}
    }
}
