2

I have some Window(s) in my application that are meant to be encapsulated in a <ContentPresenter> in its style. In this style, I build a template where I have some custom userControls like a custom titleBar and somewhere, the ContentPresenter containing the content of any of the window(s). The goal is to extract the required xaml of each of the Window and put in a template in the style. Which is exactly what wpf is meant to be used for.

Then I wanted to have an event raised from all of those window(s) when the user clicks on its content anywhere. So I added in the style:

<ContentPresenter PreviewMouseDown="somethingMouseDowned" />

Note that, at first, I applied this to the grisd inside the window(s) and, in code behind (xaml.cs), I handled the event, doing what I wanted and everything was fine.

But I want the event handling to be invisible from the window(s). That is why I went with putting the PreviewMouseDown in the style. I also don't want any handling code in my code behind.

The problem is that I don't know how to handle the event somewhere else than in the code behind. I need alternatives.

Thanks in advance

Xavier Huppé
  • 544
  • 1
  • 4
  • 17
  • What kind of control? Is this a User Control(WPF)? – paparazzo Feb 28 '14 at 15:16
  • 1
    You seem to have several severe misconceptions about how XAML in general works. I suggest you start by the basics and follow some `"Hello, World!"` type of tutorials in WPF XAML before trying to create complex composite, custom-styled UIs. – Federico Berasategui Feb 28 '14 at 15:20
  • I re-wrote my question. Please reconsider it @HighCore Thanks. – Xavier Huppé Feb 28 '14 at 15:36
  • 2
    Have you considered using the Command system? In the MVVM (Model-View-ViewModel) pattern, the goal is to put no code in the code-behind at all and have all of the logic in the ViewModel object. MVVM makes use of the Command system to accomplish that. – Tony Vitabile Feb 28 '14 at 15:48

3 Answers3

1

If you have static event handler in other class try using
{x:Static anotherClass.somethingMouseDowned}

Anton
  • 718
  • 6
  • 11
1

You can also make use of AttachedProperty (EventToCommand) and bind this event to a command (ICommand) in viewmodel. Here is the code:

 public static class ContenPreviewMouseDownCommandBinding
    {
        public static readonly DependencyProperty CommandProperty =
            DependencyProperty.RegisterAttached("Command", typeof (ICommand), typeof (ContenPreviewMouseDownCommandBinding), 
            new PropertyMetadata(default(ICommand), HandleCommandChanged));

        private static void HandleCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var contentPresenter = d as ContentPresenter;
            if(contentPresenter!=null)
            {
                contentPresenter.PreviewMouseDown += new MouseButtonEventHandler(contentPresenter_PreviewMouseDown);
            }
        }

        static void contentPresenter_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            var contentPresenter = sender as ContentPresenter;
            if(contentPresenter!=null)
            {
                var command = GetCommand(contentPresenter);
                command.Execute(e);
            }            
        }

        public static void SetCommand(ContentPresenter element, ICommand value)
        {
            element.SetValue(CommandProperty, value);
        }

        public static ICommand GetCommand(ContentPresenter element)
        {
            return (ICommand) element.GetValue(CommandProperty);
        }
    }

in XAML:

   <ContentPresenter CommandBindings:ContenPreviewMouseDownCommandBinding.Command="{Binding Path=AnyCommandInScopeOfDataContext}">

                </ContentPresenter>
Manish Basantani
  • 16,931
  • 22
  • 71
  • 103
0

At some level, you are going to have to have some code in your codebehind to do something to handle that event. However, to minimize the amount of code in your codebehind, you can utilize the MVP pattern and encapsulate all of your event handling into your presenter classes, or you can utilize some other toolset, such as Galasoft MVVMLight Messenger as described in this SO discussion.

Community
  • 1
  • 1
Joe Brunscheon
  • 1,949
  • 20
  • 21