Overriding a templated Button's Command in WPF?

It's a dirty hack, but you can bind the inner button's Command property to the templated parent's Tag property instead. That way you 'hide' the command from the containing button, but the inner button secretly knows how to get to it.

Up vote 4 down vote favorite share g+ share fb share tw.

I have a ControlTemplate that I'm using to change the appearance and behavior of several Buttons. I only want part of the Button to be clickable and to execute the bound Command, so I added a Button within the ControlTemplate and bound it to the template's Command property. The problem I'm having is that since I am defining the Command binding on the templated Button, it executes regardless of which part of the template I am clicking.

In the below example you can click the Border and execute the Command. How do I change it so that the Command only executes when you click the Button within the template? ... I don't think I can template a different element (like the Border) because I still need to pass in the Command somehow, and attached properties would still give me the same behavior.

Wpf templates xaml button command link|improve this question edited Sep 16 '11 at 17:13Dave Clemmer1,78331030 asked Jan 18 '10 at 20:12emddudley4,57411973 88% accept rate.

It's a dirty hack, but you can bind the inner button's Command property to the templated parent's Tag property instead. That way you 'hide' the command from the containing button, but the inner button secretly knows how to get to it. You can leave the CommandParameter as is, since it has no effect by itself.

Another alternative is to re-template another control, such as Border and use a RoutedCommand on the Button inside the template. You could bind it to one of the built-in RoutedCommands, such as ApplicationCommands. Save and in the XAML for the containing control, add a CommandBinding for that particular command.

Let me know if some code would be helpful.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions