0

I have the problem that I want to add an event from XAML directly to another class. The standard class, which is used, is the MainWindow.

In my situation I want to define, which class should be used for the event.

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }
    private void Window_Closing_Event(object sender, System.ComponentModel.CancelEventArgs e)
    {
    }
}

public class differentClass
{
    public differentClass()
    {
    }
    private void Window_Closing_Event(object sender, System.ComponentModel.CancelEventArgs e)
    {
    }
}

Maybe someone can help me, how I can use the event from the second class without any code in the MainWindow.

jommil
  • 21
  • 7
  • [Please do not upload images of code/errors](https://meta.stackoverflow.com/q/285551) but provide it as text – Klaus Gütter Jun 21 '21 at 05:12
  • The most universal solution is probably the top answer on this similar question: https://stackoverflow.com/questions/7877532/wpf-event-binding-from-view-to-viewmodel but really it depends entirely on what you'd like to actually do and why you want to avoid codebehind code. The easiest way is usually just biting the bullet and having the event routing handled in the constructor. – T.Schwarz Jun 21 '21 at 07:29
  • My goal is to make a cut between events and View, so I want to call the event (events) from another class in a different folder inside the project. The need is to have a clean MainWindow.xaml.cs without any event. My main question is how to write the xaml code in that way, that I can call the event from the differentClass class, or better case the event will be created automatically in the differentClass class. Thanks for your help – jommil Jun 21 '21 at 07:59
  • Why would you want to have such a goal? If you attach an event handler in XAML Designer, the handler method will inevitably be generated in the code behind class of the view. This is just the way the designer is working. The generated handler method however could delegate to whatever class or object you like, and would therefore not contain more than a single line of code. – Clemens Jun 21 '21 at 08:06
  • I want to have every event implemented in the class, where the functionality (methods) of the event is included. For example I want to have the event for a login_button in the same Subfolder as the login itself. – jommil Jun 21 '21 at 09:24
  • That makes no sense. The Click event handler of a Button belongs to the view that contains the Button, not the view that is navigated to when you click the Button. – Clemens Jun 21 '21 at 09:46
  • I know and agree with you exactly. The only thing, what I want to reach is to have view update in the MainWindow and every controlling or logic outside – jommil Jun 22 '21 at 04:22

1 Answers1

0

There is a Behavior class for this purpose. You will need to add the reference to the System.Windows.Interactivity in the project: How to add System.Windows.Interactivity to project?

using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;

public class CustomWindowHandlerBehavior: Behavior<Window>
{
    protected override void OnAttached()
    {
        base.OnAttached();
        AssociatedObject.Closing+= Window_Closing_Event;
    }

    protected override void OnDetaching()
    {
        AssociatedObject.Closing-= Window_Closing_Event;
        base.OnDetaching();
    }

    private void Window_Closing_Event(object sender, System.ComponentModel.CancelEventArgs e)
    {
        //...
    }
}

using this behavior in XAML:

<Window
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity">
    <i:Interaction.Behaviors>
        <local:CustomWindowHandlerBehaviour />
    </i:Interaction.Behaviors>
<Window/>
Rekshino
  • 6,954
  • 2
  • 19
  • 44
  • This code works, but only if the event code exists in the MainWindow.xaml.cs. My need is that there are no events in the MainWindowXaml.cs and only the events are in the second class. Maybe that would be possible. Thanks for this solution. – jommil Jun 21 '21 at 09:01
  • You can/should put `CustomWindowHandlerBehavior` to the separate file. It does work without event handler in MainWindowXaml.cs. Have you adjusted the XAML accordingly? – Rekshino Jun 21 '21 at 09:44
  • yes I added the xmlns:i input and added the – jommil Jun 21 '21 at 10:01
  • Which `Buton`?? Interaction for the `Window` has to be added to the ``, not to the Button. Please stay by your question, do not try to ask all questions in one. – Rekshino Jun 21 '21 at 10:13
  • To try, I added it to the Window and the same error occurs. There the console writes: "MainWindow" does not contain a definition for "{Event}" and no accessible extension method "{Event}" accepting a first argument of type "MainWindow" could be found (Are you missing a using directive or an assembly reference?) {Event} replaced with specific event handler name – jommil Jun 21 '21 at 10:37
  • Have you added reference to the `System.Windows.Interactivity` in the project? – Rekshino Jun 21 '21 at 12:28
  • Yes, `System.Windows.Interactivity` has been added to the References – jommil Jun 21 '21 at 12:53
  • These may be errors from your previous trials. Clean up the code, delete all unnecessary calls(also in XAML). It should work and works by my. _"{Event} replaced with specific event handler name"_ - I don't understand what do you mean with it. – Rekshino Jun 21 '21 at 13:06
  • I had cleaned it up but no change, but previously thank you for your help. With the variable {Event} I mean that could be something like CloseWindowEvent or ClickButtonEvent or HoverImageEvent. There I only want to show you that my error box shows me a message, where this event is not found. – jommil Jun 22 '21 at 04:17