views:

298

answers:

1

I've been attempting to bind to buttons within a DataTemplate without much success. The button does not fire. Button Click works successfully outside of the DataTemplate. Yet if I create a Click="button_click" the click button is fired. The Button Content binds perfectly as well.

Example to illustrate..

Why does the command not fire? Advice on how this should be handled this? The example is a simplified version of my real problem, I am currently not using a Listbox. I only recreated the same problem with a listbox..

A: 

I asked the same question once before, came up with the answer myself, see this question:

http://stackoverflow.com/questions/2118395/how-to-connect-a-button-in-a-silverlight-listitem-datatemplate-in-a-resourcedict

The solution seems to be to use 'nested' ViewModels here:

In other words, rather than have my ListBox bind directly to a collection of DTOs/business objects (as I was doing above) I instead created a simple ViewModel to wrap each DTO, and have the command on it, rather than on the original, top-level VM.

This seem like a sln?

Bobby
Yup it is for sure. I actually ended up implementing the exact same thing. The real problem I had was with a panel control, and I wanted to remove a panel using a button on the panel. Using the parents view model bindings became less than clean in my opinion. Required a key or index to bind to on the command parm, and I'd rather just not use a command parm at all. Every time I think about using it, I question my method of attack. Usually if I need it, I'm doing something wrong.Thanks for the reply.
deliberative assembly