嗯,我想你錯了MouseDown
事件的財產。沒有IsMouseDown
屬性,但存在類似的IsPressed
屬性,但僅適用於繼承ButtonBase
的類。如果你想保持你的代碼清潔,你應該只使用代碼隱藏事件或寫一個附加屬性。
這是你如何做到的。創建類:
using System;
using System.Windows;
namespace Mrpyo
{
public static class MouseDownHelper
{
public static readonly DependencyProperty IsEnabledProperty = DependencyProperty.RegisterAttached("IsEnabled",
typeof(bool), typeof(MouseDownHelper), new FrameworkPropertyMetadata(false, new PropertyChangedCallback(OnNotifyPropertyChanged)));
public static void SetIsEnabled(UIElement element, bool value)
{
element.SetValue(IsEnabledProperty, value);
}
public static bool GetIsEnabled(UIElement element)
{
return (bool)element.GetValue(IsEnabledProperty);
}
private static void OnNotifyPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var element = d as UIElement;
if (element != null && e.NewValue != null)
{
if ((bool)e.NewValue)
{
Register(element);
}
else
{
UnRegister(element);
}
}
}
private static void Register(UIElement element)
{
element.PreviewMouseDown += element_MouseDown;
element.PreviewMouseLeftButtonDown += element_MouseLeftButtonDown;
element.MouseLeave += element_MouseLeave;
element.PreviewMouseUp += element_MouseUp;
}
private static void UnRegister(UIElement element)
{
element.PreviewMouseDown -= element_MouseDown;
element.PreviewMouseLeftButtonDown -= element_MouseLeftButtonDown;
element.MouseLeave -= element_MouseLeave;
element.PreviewMouseUp -= element_MouseUp;
}
private static void element_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
var element = e.Source as UIElement;
if (element != null)
{
SetIsMouseDown(element, true);
}
}
private static void element_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
var element = e.Source as UIElement;
if (element != null)
{
SetIsMouseLeftButtonDown(element, true);
}
}
private static void element_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
var element = e.Source as UIElement;
if (element != null)
{
SetIsMouseDown(element, false);
SetIsMouseLeftButtonDown(element, false);
}
}
private static void element_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
var element = e.Source as UIElement;
if (element != null)
{
SetIsMouseDown(element, false);
SetIsMouseLeftButtonDown(element, false);
}
}
internal static readonly DependencyPropertyKey IsMouseDownPropertyKey = DependencyProperty.RegisterAttachedReadOnly("IsMouseDown",
typeof(bool), typeof(MouseDownHelper), new FrameworkPropertyMetadata(false));
public static readonly DependencyProperty IsMouseDownProperty = IsMouseDownPropertyKey.DependencyProperty;
internal static void SetIsMouseDown(UIElement element, bool value)
{
element.SetValue(IsMouseDownPropertyKey, value);
}
public static bool GetIsMouseDown(UIElement element)
{
return (bool)element.GetValue(IsMouseDownProperty);
}
internal static readonly DependencyPropertyKey IsMouseLeftButtonDownPropertyKey = DependencyProperty.RegisterAttachedReadOnly("IsMouseLeftButtonDown",
typeof(bool), typeof(MouseDownHelper), new FrameworkPropertyMetadata(false));
public static readonly DependencyProperty IsMouseLeftButtonDownProperty = IsMouseLeftButtonDownPropertyKey.DependencyProperty;
internal static void SetIsMouseLeftButtonDown(UIElement element, bool value)
{
element.SetValue(IsMouseLeftButtonDownPropertyKey, value);
}
public static bool GetIsMouseLeftButtonDown(UIElement element)
{
return (bool)element.GetValue(IsMouseLeftButtonDownProperty);
}
}
}
然後在你的風格:
<Setter Property="local:MouseDownHelper.IsEnabled" Value="True"/>
<Style.Triggers>
<Trigger Property="local:MouseDownHelper.IsMouseLeftButtonDown" Value="True">
<!-- ... -->
</Trigger>
</Style.Triggers>
當然在你的XAML文件中添加命名空間和(頂一下):
xmlns:local="clr-namespace:Mrpyo"
不知道,但'MouseDown'不列爲財產在你提供的鏈接。 'OnMouseDown()'被列爲響應事件調用的方法,但不是屬性。 –