Search code examples
c#.netwpfvisual-studiomodular

Set event tunneling default on every control


I'm planing to develop an application with modular content. Any modular content is just a custom user control. The application is also required to have the possibility to disable any user input, like click events. Is it possible to set event tunneling to default, so i can do

event.Handled = true;

on my host and prevent the event to reach it's source (Modular content)? So regardless of the content i always want the events to be tunneled rather than bubbled.

Here's a simple sketch i made to demonstrate my needsBasic application sketch

Maybe there's a "better" way to do so. If so please share your thoughts.


Solution

  • If I understand you correctly you could handle the PreviewMouseLeftButtonDown event of the host window and set the MouseButtonEventArgs.Handled property to true to prevent the routed event from tunneling down to the modular content:

    public partial class HostWindow : Window
    {
        public HostWindow()
        {
            InitializeComponent();
            PreviewMouseLeftButtonDown += (s, e) => e.Handled = true;
        }
    }
    

    This should effectively disable left-button mouse input in the UserControl. There are several other Preview* events as well: https://msdn.microsoft.com/en-us/library/ms752279(v=vs.110).aspx