2010-10-20 54 views
2

我有一个ListBox与ItemsTemplate设置。如何捕获当项目模板化时单击ListboxItem?

现在我想抓住Ctrl +左键单击ListBoxItem。

我发现KeyBoard类应该给我修饰键。现在我该如何获得ListBoxItem上的点击事件?更好的是,我如何将它绑定到ICommand。

我发现了一些碎片,但不知道如何连接它们。看来InputBinding似乎可以帮助我或EventSetter

回答

4

下面是一个简单的例子,它使用ListBoxItem的样式中的EventSetter处理Ctrl + PreviewMouseLeftButtonDown。这可能是你想要的。

XAML:

<ListBox> 
    <ListBox.ItemContainerStyle> 
     <Style TargetType="ListBoxItem"> 
      <EventSetter Event="PreviewMouseLeftButtonDown" Handler="ListBoxItem_PreviewMouseLeftButtonDown"/> 
     </Style> 
    </ListBox.ItemContainerStyle> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <Button Content="{Binding}"/> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
    <s:String>Item1</s:String> 
    <s:String>Item2</s:String> 
    <s:String>Item3</s:String> 
    <s:String>Item4</s:String> 
</ListBox> 

代码隐藏:

void ListBoxItem_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
{ 
    if ((Keyboard.Modifiers & ModifierKeys.Control) > 0) 
    { 
     Console.WriteLine((sender as ListBoxItem).Content.ToString()); 
     e.Handled = false; 
    } 
} 

将其绑定到一个ICommand,您可以使用附加的行为像EventToCommand行为讨论here

编辑:

为了解决您的意见,处理点击事件会有点棘手,因为两件事情ListBoxItem中:1)ListBoxItem中没有一个点击事件,和2) ListBoxItem在内部处理一些MouseEvent。无论如何,我想出了一个模拟的,附加的ClickEvent来使它工作。见下文。希望它有效。

public class AttachedEvents 
{ 
    private static readonly DependencyProperty IsTriggerEnabledProperty = 
     DependencyProperty.RegisterAttached("IsTriggerEnabled", typeof(bool), typeof(FrameworkElement), new FrameworkPropertyMetadata(false)); 

    public static readonly RoutedEvent ClickEvent; 

    static AttachedEvents() 
    { 
     try 
     { 
      ClickEvent = EventManager.RegisterRoutedEvent("Click", 
                 RoutingStrategy.Bubble, 
                 typeof(RoutedEventHandler), 
                 typeof(FrameworkElement)); 
     } 
     catch (Exception ex) 
     { } 
    } 


    private static void SetIsTriggerEnabled(FrameworkElement element, bool value) 
    { 
     if (element != null) 
     { 
      element.SetValue(IsTriggerEnabledProperty, value); 
     } 
    } 

    private static bool GetIsTriggerEnabled(FrameworkElement element) 
    { 
     return (element != null) ? (bool)element.GetValue(IsTriggerEnabledProperty) : false; 
    } 

    public static void AddClickHandler(DependencyObject o, RoutedEventHandler handler) 
    { 
     FrameworkElement element = (FrameworkElement)o; 
     element.AddHandler(ClickEvent, handler); 
     element.MouseLeftButtonUp += new System.Windows.Input.MouseButtonEventHandler(SimulatedClick_MouseLeftButtonUp); 
     element.PreviewMouseLeftButtonDown += new System.Windows.Input.MouseButtonEventHandler(SimulatedClick_MouseLeftButtonDown); 
    } 

    public static void RemoveClickHandler(DependencyObject o, RoutedEventHandler handler) 
    { 
     FrameworkElement element = (FrameworkElement)o; 
     element.RemoveHandler(ClickEvent, handler); 
     element.MouseLeftButtonUp -= new System.Windows.Input.MouseButtonEventHandler(SimulatedClick_MouseLeftButtonUp); 
     element.PreviewMouseLeftButtonDown -= new System.Windows.Input.MouseButtonEventHandler(SimulatedClick_MouseLeftButtonDown); 
    } 

    static void SimulatedClick_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e) 
    { 
     FrameworkElement element = (FrameworkElement)sender; 
     UpdateIsTriggerSet(element); 
     Mouse.Capture(element); 
    } 

    static void SimulatedClick_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e) 
    { 
     FrameworkElement element = (FrameworkElement)sender; 

     bool isTriggerSet = (bool)element.GetValue(IsTriggerEnabledProperty); 

     // update the trigger set flag 
     UpdateIsTriggerSet(element); 

     //release the mouse capture 
     Mouse.Capture(null); 

     // if trigger is set and we are still over the element then we fire the click event 
     if (isTriggerSet && IsMouseOver(element)) 
     { 
      element.RaiseEvent(new RoutedEventArgs(ClickEvent, sender)); 
     } 

    } 

    private static bool IsMouseOver(FrameworkElement element) 
    { 
     Point position = Mouse.PrimaryDevice.GetPosition(element); 
     if (((position.X >= 0.0) && (position.X <= element.ActualWidth)) && ((position.Y >= 0.0) && (position.Y <= element.ActualHeight))) 
     { 
      return true; 
     } 
     else 
     { 
      return false; 
     } 
    } 

    private static void UpdateIsTriggerSet(FrameworkElement element) 
    { 
     Point position = Mouse.PrimaryDevice.GetPosition(element); 
     if (((position.X >= 0.0) && (position.X <= element.ActualWidth)) && ((position.Y >= 0.0) && (position.Y <= element.ActualHeight))) 
     { 
      if (!(bool)element.GetValue(IsTriggerEnabledProperty)) 
      { 
       element.SetValue(IsTriggerEnabledProperty, true); 
      } 
     } 
     else if ((bool)element.GetValue(IsTriggerEnabledProperty)) 
     { 
      element.SetValue(IsTriggerEnabledProperty, false); 
     } 
    } 
} 

示例用法如下所示。我似乎无法在XAML中设置附加事件(我不知道为什么),所以我不得不在这里做一个解决方法。我所做的就是等待直到ListBoxItem被加载,然后在代码隐藏中附加事件处理程序。

XAML:

<ListBox> 
    <ListBox.ItemContainerStyle> 
     <Style TargetType="ListBoxItem"> 
      <EventSetter Event="Loaded" Handler="OnLoaded"/> 
     </Style> 
    </ListBox.ItemContainerStyle> 
... 
</ListBox> 

代码隐藏:

void OnLoaded(object sender, RoutedEventArgs e) 
{ 
    AttachedEvents.AddClickHandler((sender as ListBoxItem), HandleClick); 
} 

void HandleClick(object sender, RoutedEventArgs e) 
{ 
    if ((Keyboard.Modifiers & ModifierKeys.Control) > 0) 
    { 
     Console.WriteLine("Ctrl + Clicked!"); 
    } 
} 
+0

的MouseLeftButtonDown是不是一个真正的点击。 – Kugel 2010-10-20 20:54:20

+0

你说得对。我只是认为MouseDown会足够好。无论如何,请参阅编辑。这是一个相当长的解决方案,但我希望它能起作用。 – ASanch 2010-10-20 21:52:10

+0

谢谢你的洞察力。我从你的帖子中学到了很多东西。 – Kugel 2010-10-21 12:48:22

相关问题