WPF Question Regarding Event Handling in XAML

Question: Greetings Gurus,

I am just learning WPF and working through some of the sample projects and tutorials on the web.  I have a question:

Why is it necessary to add the event handler in the XAML, when you are adding it to the code behind already?  For example, in the current tutorial that I am working through, I am instructed to add the following:

 <!-- View report button -->
  <Button Grid.Column="1" Grid.Row="3" Margin="0,10,0,0" Width="125"
  Height="25" HorizontalAlignment="Right" Click="Button_Click">View</Button>

However, I noticed that if I don't add the "Click=Button_Click" in the XAML and create the click event in the code behind, the application still works as expected.  Is it necessary to do both?   If so, can you help me to understand why.

Thank you
Splinter_2K10Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

saraganiCommented:
Hi, there are 2 ways to register an event in C#, one of them is in XAML, just like you wrote:
Click="Button_Click"

This is like saying to the code: When the button is clicked, call the function Button_Click on the current UI.

There is also a way of registering the event in Code-Behind. For that, the button will need to have a name (x:Name), so you could access it in code behind:

button.Click += Button_Click

You don't need to have both since that would only case the "Button_Click" function to be called twice on each click.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Splinter_2K10Author Commented:

Ok, I understand now.  After looking at the example further, they did not have the "Handles Button_Click" statement in the code behind sub.  Therefore the event was only firing from the XAML handler.

Thank you for the quick response.

saraganiCommented:
As a side note, your little XAML code has almost all the possible mistakes. This is not the right way of writing a propper code in XAML.

1) Avoid as much as possible on having fixes sizes (unless needed).
2) Avoid writing Code-Behind... you can still do it if writing something in Code-Behind takes 2 lines and doing the same thing in your ViewModel is much much longer and complicated to do...
3) Avoid using Events, since events require Code-Behind.


So now you are probably wondering "how the hell would I do anything and interact with my code without events???"

Well, for instance, use Commands for executing Click.
Buttons have a property called Command. By giving it a Binding to a Command, the Click code would be executed on your ViewModel level other than your UI level (which should not contain any Business-Logic).

For other events (Like mouse down, text changed etc...) you can use Blend 4. It has Behaviors which allows you to attack an event to a behavior (For example, CallMethodAction).

With CallMethodAction you can call a void function on your ViewModel.
You can have the function as parameterless, and you can also give it the same signature as the event handler (For example, on Mouse Down it is: object sender, MouseEventAgrs e), for example:

public void MouseDownBehavior(object sender, MouseEventAgrs e)
{
}

If you are not using Blend or don't want to have all the references to it's Interaction dlls (Microsoft.Windows.Interaction.dll or something like that), then you can create behavior by yourself that will let you do the same thing
Splinter_2K10Author Commented:

Thank you for the additional information.   I've been reading about the MVVM design pattern and will be tackling that once I get more familiar with WPF in general.  

I have a lot of experience in VB.NET Windows Forms, but WPF seems like a fairly steep learning curve.   At the same time, I'm trying to unlearn some of my sloppy design practices from the past.  :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.