mirror of
https://github.com/ckaczor/Common.Wpf.git
synced 2026-01-14 01:25:37 -05:00
Initial commit
This commit is contained in:
32
ExtendedListBoxControl/ExtendedListBox.cs
Normal file
32
ExtendedListBoxControl/ExtendedListBox.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
|
||||
namespace Common.Wpf.ExtendedListBoxControl
|
||||
{
|
||||
public class ExtendedListBox : ListBox
|
||||
{
|
||||
protected override DependencyObject GetContainerForItemOverride()
|
||||
{
|
||||
return new ExtendedListBoxItem();
|
||||
}
|
||||
|
||||
private int _lastSelectedIndex = -1;
|
||||
|
||||
protected override void OnSelectionChanged(SelectionChangedEventArgs e)
|
||||
{
|
||||
base.OnSelectionChanged(e);
|
||||
|
||||
if (SelectedIndex == -1 && _lastSelectedIndex != -1)
|
||||
{
|
||||
int itemCount = Items.Count;
|
||||
|
||||
if (_lastSelectedIndex >= itemCount)
|
||||
_lastSelectedIndex = itemCount - 1;
|
||||
|
||||
SelectedIndex = _lastSelectedIndex;
|
||||
}
|
||||
|
||||
_lastSelectedIndex = SelectedIndex;
|
||||
}
|
||||
}
|
||||
}
|
||||
75
ExtendedListBoxControl/ExtendedListBoxItem.cs
Normal file
75
ExtendedListBoxControl/ExtendedListBoxItem.cs
Normal file
@@ -0,0 +1,75 @@
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Input;
|
||||
|
||||
using Common.Wpf.Extensions;
|
||||
|
||||
namespace Common.Wpf.ExtendedListBoxControl
|
||||
{
|
||||
public class ExtendedListBoxItem : ListBoxItem
|
||||
{
|
||||
#region Helper properties
|
||||
|
||||
public ExtendedListBox ParentListBox
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.GetAncestor<ExtendedListBox>();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Fix selection handling for multiple selection
|
||||
|
||||
private bool _fireMouseDownOnMouseUp;
|
||||
private SelectionMode? _selectionMode;
|
||||
|
||||
private SelectionMode SelectionMode
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_selectionMode == null)
|
||||
{
|
||||
// Get the parent list box
|
||||
ListBox listBox = ParentListBox;
|
||||
|
||||
// Cache the selection mode
|
||||
_selectionMode = listBox.SelectionMode;
|
||||
}
|
||||
|
||||
return _selectionMode.Value;
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
|
||||
{
|
||||
// If the item is already selected we want to ignore the mouse down now and raise it on mouse up instead
|
||||
if (SelectionMode != SelectionMode.Single && IsSelected)
|
||||
{
|
||||
_fireMouseDownOnMouseUp = true;
|
||||
return;
|
||||
}
|
||||
|
||||
// Call the normal mouse down
|
||||
base.OnMouseLeftButtonDown(e);
|
||||
}
|
||||
|
||||
protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
|
||||
{
|
||||
// If we ignored the earlier mouse down we need to fire it now
|
||||
if (SelectionMode != SelectionMode.Single && _fireMouseDownOnMouseUp)
|
||||
{
|
||||
// Call the normal mouse down
|
||||
base.OnMouseLeftButtonDown(e);
|
||||
|
||||
// Clear the flag
|
||||
_fireMouseDownOnMouseUp = false;
|
||||
}
|
||||
|
||||
// Call the normal mouse up
|
||||
base.OnMouseLeftButtonUp(e);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user