Hello,
I want to put all my events in one class file and call them in a main window
following is my code in mainwindow.xaml file

<Window x:Class="WPF123.MainWindow" 
xmlns:Local="clr-namespace:WPF123">

 <Window.CommandBindings >
<CommandBinding Command="Local:MyCommands.Print" Executed="PrintCommandHandler" CanExecute="CommandBinding_CanExecute" />
</Window.CommandBindings>

...

<Button SmallImageSource="Images\Print.png" ToolTipTitle="Print" Command="Local:MyCommands.Print"  />
</Window>

mainwindow.xaml.cs

public MainWindow()
        {
            InitializeComponent();
            MyCommands.BindCommandsToWindow(this);
        }

And I created a Class called MyCommands followings is the code

public static class MyCommands
    {
        private static RoutedUICommand Print;

        static MyCommands()
        {
            Print = new RoutedUICommand("Print","Print",typeof(MyCommands));
        }
        public static void BindCommandsToWindow(Window window)
        {
            window.CommandBindings.Add(new CommandBinding(Print,PrintCommandHandler));
            
        }
        public static void PrintCommandHandler(object sender, ExecutedRoutedEventArgs e)
        {
            
            PrintDialog prtdialog = new PrintDialog();
            prtdialog.ShowDialog();
            
        }
        public static  void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }
    }

What did I miss on button line it says I get error saying "type Local:Mycommands cannot be found"...

Well, am also new to WPF, but I doubt you can do so.
it is something similar to normal winforms, your events will be related in the designer.cs file where your controls are placed in designer file.
works the same here too.
you can seperate a layer of the logic if you want less code in ur xaml***.cs file.

Hello, i will be off topic but ihave to inform you...

dont spend your time with WPF application's , WPF dont have any feature and you can't do much things.

YG: im joking ;p, WPF is also C#, but no ones create WPF Application's, wpf is just for kids who wanna try paint some beutyfull controls, :) no one will ask you to make a pikachu application :)

Edited 5 Years Ago by Panathinaikos22: n/a

WPF is next generation rich application development possible .It has so many features compare to normal windows application.It has variety controls as well typical databinding conecpts .Graphs,report,Chart applications so many things we can develop with more interactive .

I work for a company who creates a multitude of software for consumer facing business hardware. Every project I've done this year has been in WPF and I wouldn't choose WinForms again, unless it really is an incredibly simple application.

Discounting WPF as a serious development API is incredibly short sighted.

As for your problem, I have a feeling that you have to contain your events inside the calling window class. I've never tried to do this, but it would make sense if that you can't.

This article has been dead for over six months. Start a new discussion instead.