Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3305848
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:13:04+00:00 2026-05-17T21:13:04+00:00

I have a ListBox with ItemsTemplate set. Now I want to catch Ctrl +

  • 0

I have a ListBox with ItemsTemplate set.

Now I want to catch Ctrl + Left click on a ListBoxItem.

I found KeyBoard class that should give me modifier keys. Now how do I get the click event on the ListBoxItem? Even better, how do I bind it to ICommand.

I found some bits and pieces but don’t know how to connect them. It seems InputBinding seems could help me or EventSetter.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T21:13:05+00:00Added an answer on May 17, 2026 at 9:13 pm

    Below is a simple example that handles Ctrl + PreviewMouseLeftButtonDown using an EventSetter in the ListBoxItem’s Style. This is probably what you want.

    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>
    

    Code-Behind:

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

    To bind it to an ICommand, you can use an attached behavior like the EventToCommand behavior discussed here.

    EDIT:

    To address your comment, handling the Click-event will be a bit tricky for the ListBoxItem because of two things: 1) The ListBoxItem doesn’t have a click event, and 2) The ListBoxItem internally handles some of the MouseEvents. Anyway, I came up with a simulated, attached ClickEvent to make it work. See below. Hope it works.

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

    Sample usage is shown below. I can’t seem to set the attached event in XAML (I’m not sure why) so I had to do a workaround here. What I do is wait ’til the ListBoxItem gets loaded and Attach the event handler in the code-behind.

    XAML:

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

    Code-Behind:

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ListBox that each of its items has a button, I set
I have a ListBox that has a style defined for ListBoxItems. Inside this style,
I have a ListBox which displays items of variable height. I want to show
I have a listbox that is databound to a Collection of objects. The listbox
I have a ListBox that when in focus, and when I have an item
I have an ObservableCollection<Object> that contains two different types. I want to bind this
I have a little issue. I have a a ListBox that is bound to
I have a UserControl that I've added a Dependency Property to: public partial class
I have a listbox where the items contain checkboxes: <ListBox Style={StaticResource CheckBoxListStyle} Name=EditListBox> <ListBox.ItemTemplate>
I have a databound and itemtemplated ListBox: <ListBox x:Name=lbLista ScrollViewer.VerticalScrollBarVisibility=Visible> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation=Horizontal>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.