|
namespace Aviad.WPF.Controls
{
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
public static class ListBoxItemBehavior
{
#region fields
// Using a DependencyProperty as the backing store for SelectOnMouseOver.
// This enables animation, styling, binding, etc...
public static readonly DependencyProperty SelectOnMouseOverProperty =
DependencyProperty.RegisterAttached("SelectOnMouseOver",
typeof(bool),
typeof(ListBoxItemBehavior),
new UIPropertyMetadata(false, OnSelectOnMouseOverChanged));
#endregion fields
#region methods
public static bool GetSelectOnMouseOver(DependencyObject obj)
{
return (bool)obj.GetValue(SelectOnMouseOverProperty);
}
public static void SetSelectOnMouseOver(DependencyObject obj, bool value)
{
obj.SetValue(SelectOnMouseOverProperty, value);
}
private static void OnSelectOnMouseOverChanged(DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
ListBoxItem lbi = d as ListBoxItem;
if (lbi == null) return;
bool bNew = (bool)e.NewValue, bOld = (bool)e.OldValue;
if (bNew == bOld) return;
if (bNew)
lbi.MouseEnter += new MouseEventHandler(lbi_MouseEnter);
else
lbi.MouseEnter -= lbi_MouseEnter;
}
private static void lbi_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
ListBoxItem lbi = (ListBoxItem)sender;
lbi.IsSelected = true;
var listBox = ListBox.ItemsControlFromItemContainer(lbi);
FrameworkElement focusedElement =
(FrameworkElement)FocusManager.GetFocusedElement(FocusManager.GetFocusScope(listBox));
if (focusedElement != null && focusedElement.IsDescendantOf(listBox))
lbi.Focus();
}
#endregion fields
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
The Windows Presentation Foundation (WPF) and C# are among my favorites and so I developed
Edi
and a few other projects on
GitHub. I am normally an algorithms and structure type but WPF has such interesting UI sides that I cannot help myself but get into it.
https://de.linkedin.com/in/dirkbahle