This solution lets you set certain elements within your ListViewItem template not trigger a row selection by blocking the click event bubbling.
Attach this property to an element.
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace YourNamespaceName
{
public class CancelMouseBubbling : DependencyObject
{
public static readonly DependencyProperty ActiveProperty = DependencyProperty.RegisterAttached(
"Active",
typeof(bool),
typeof(CancelMouseBubbling),
new PropertyMetadata(false, ActivePropertyChanged));
/// <summary>
/// Subscribe to the events we need.
/// </summary>
private static void ActivePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var element = d as FrameworkElement;
if (element != null)
{
if ((e.NewValue as bool?).GetValueOrDefault(false))
{
element.PreviewMouseLeftButtonDown += ElementOnPreviewMouseLeftButtonDown;
element.MouseLeftButtonDown += ElementOnMouseLeftButtonDown;
}
else
{
element.PreviewMouseLeftButtonDown -= ElementOnPreviewMouseLeftButtonDown;
element.MouseLeftButtonDown -= ElementOnMouseLeftButtonDown;
}
}
}
/// <summary>
/// Block some events from bubbling at the OriginalSource.
/// </summary>
private static void ElementOnPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs mouseButtonEventArgs)
{
if (mouseButtonEventArgs.Source is Panel)
{
mouseButtonEventArgs.Handled = true;
}
}
/// <summary>
/// Block all clicks from going past the element CancelMouseBubbling is set on
/// </summary>
private static void ElementOnMouseLeftButtonDown(object sender, MouseButtonEventArgs mouseButtonEventArgs)
{
mouseButtonEventArgs.Handled = true;
}
[AttachedPropertyBrowsableForChildrenAttribute(IncludeDescendants = false)]
[AttachedPropertyBrowsableForType(typeof(FrameworkElement))]
public static bool GetActive(DependencyObject @object)
{
return (bool)@object.GetValue(ActiveProperty);
}
public static void SetActive(DependencyObject @object, bool value)
{
@object.SetValue(ActiveProperty, value);
}
}
}
Declare a namespace so that you can access it:
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:ut="clr-namespace:YourNamespaceName"></UserControl>
And attach it to an element.
<Border ut:CancelMouseBubbling.Active="True" Background="#55171717">
...
</Border>