2012-05-19 30 views
10

Tại sao IsMouseOver được nhận dạng dưới dạng trình kích hoạt kiểu WPF và MouseDown không -given rằng cả hai thuộc tính UIElement hợp lệ là seen here -. Kích hoạt đầu tiên hoạt động tốt nhưng kích hoạt thứ hai thậm chí không biên dịch.Tại sao IsMouseOver được nhận dạng và MouseDown không phải là (Trình kích hoạt kiểu Wpf)?

<Style.Triggers> 
    <Trigger Property="IsMouseOver" Value="true"> 
     <Setter Property="OpacityMask"> 
      <Setter.Value> 
       <LinearGradientBrush > 
        <GradientStop Color="Transparent" Offset="0"/> 
        <GradientStop Color="Black" Offset="0.5"/> 
        <GradientStop Color="Transparent" Offset="1"/> 
       </LinearGradientBrush> 
      </Setter.Value> 
     </Setter> 
    </Trigger> 
    <Trigger Property="MouseDown" Value="true"> 
     <Setter Property="OpacityMask"> 
      <Setter.Value> 
       <LinearGradientBrush> 
        <GradientStop Color="Black" Offset="0" /> 
        <GradientStop Color="White" Offset="1" /> 
       </LinearGradientBrush> 
      </Setter.Value> 
     </Setter> 
    </Trigger> 
</Style.Triggers> 
+1

Không biết, nhưng 'MouseDown' không được liệt kê như một thuộc tính tại liên kết mà bạn đã cung cấp. 'OnMouseDown()' được liệt kê như một phương thức được gọi để đáp ứng với một sự kiện, nhưng không phải là một thuộc tính. –

Trả lời

15

Vâng, tôi đoán bạn đang nhầm MouseDown sự kiện cho tài sản. Không có thuộc tính IsMouseDown nhưng có thuộc tính IsPressed tương tự nhưng chỉ dành cho các lớp kế thừa ButtonBase. Bạn chỉ nên sử dụng sự kiện ở phía sau mã hoặc viết thuộc tính đính kèm nếu bạn muốn giữ cho mã của bạn luôn sạch sẽ.

Đây là cách bạn thực hiện. Tạo lớp:

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); 
     } 
    } 
} 

Sau đó, trong phong cách của bạn:

<Setter Property="local:MouseDownHelper.IsEnabled" Value="True"/> 
<Style.Triggers> 
    <Trigger Property="local:MouseDownHelper.IsMouseLeftButtonDown" Value="True"> 
     <!-- ... --> 
    </Trigger> 
</Style.Triggers> 

Và tất nhiên thêm namespace trong file XAML của bạn (tìm ở đầu trang):

xmlns:local="clr-namespace:Mrpyo" 
+0

bạn có thể cung cấp ví dụ về những gì bạn đang nói với "viết thuộc tính đính kèm" không? –

+0

Tôi đang viết một lớp học dành riêng cho bạn;). Nhưng bạn phải đợi một chút ... – mrpyo

+0

Cảm ơn rất nhiều vì lớp này. Tôi đã phải sử dụng các sự kiện PreviewMouseDown và PreviewMouseUp để làm cho nó hoạt động, nhưng sau đó nó thật tuyệt vời. –

2

Bạn có thể sử dụng MouseDown Event trong Style.Triggers nhưng bạn phải sử dụng EventTrigger cho điều đó.

<EventTrigger RoutedEvent="MouseEnter"> 
    <BeginStoryboard> 
     <Storyboard> 
      ... 
     </Storyboard> 
    </BeginStoryboard> 
</EventTrigger> 

Và hãy nhớ rằng

hành động sẽ không được hoàn tác một lần với điều kiện huy động sự kiện là không còn đúng nữa.

0

Bạn có thể sử dụng PreviewMouseLeftButtonDown khi sử dụng Control.Triggers, thay thế kiểm soát với mục kiểm soát các mẫu đang được sử dụng trong:

<Grid> 
    <Grid.Triggers> 
    <EventTrigger RoutedEvent="Grid.PreviewMouseLeftButtonDown"> 
     <BeginStoryboard> 
     <Storyboard> 
      ... 
     </Storyboard> 
     </BeginStoryboard> 
    </EventTrigger> 
    </Grid.Triggers> 
</Grid> 
Các vấn đề liên quan