views:

431

answers:

1

I am having trouble trying to get a click event to fire after the command gets executed. I am trying to follow the MVVM style of development.

Here is the scenario:

In my application I have a grid of 2 by N buttons, where N is a dynamically loaded set of buttons (sometimes it'll be 2 x 2, sometimes its 5 x 9, I don't know). Initially, all the columns of buttons will be hidden except for the first column. Upon clicking the first column, a Command gets executed, and a fade in animation gets played and shows certain buttons from the second column, depending on which button was clicked in the first column. For example, clicking the top button in the first column will reveal the first four buttons in the second column, whereas the bottom button in the first column will reveal the first three buttons in the second column.

Here's the problem:

For each button, I set a CommandParameter to each of the buttons in the first column. When the command gets executed, data gets processed, and a property is set in the VM that says which button was pressed (I just use the CommandParameter again). While the data is being processed, I want to display the next set of buttons to the user. I have a click event that takes the CommandParameter from the VM and plays an animation to show the proper buttons.

However, the problem is that WPF likes to do the Click event first before the Command gets fired. This is a problem since the property in the VM that says which button was clicked doesn't get set!

So that brings me to a couple questions...

1) Is there a way to execute the command before firing the event?

2) Am I doing it right? :P

+1  A: 

Instead of using a click event handler, you could just setup DataTriggers on your buttons.

They could respond to the property in your ViewModel, hiding buttons you don't want displayed, and showing the button you do.

The nice thing about this is that your View can be done completely in XAML, and the ViewModel is just setting state (ie: I'm at this state), and the View will automatically stay updated since the binding will cause the new DataTrigger to fire.

Reed Copsey
I tried to change everything over to use datatriggers and triggering on the data binding, as well as trying to switch everything over to XAML. The problem I'm having is that all my buttons that I am loading are being done programmatically because 1) they are dynamically loaded with a background worker (this is required because i need it asynchronously loaded and the button image are loaded over the web) and 2) positioning of the buttons are done in a viewport3d using Viewport2DVisual3D randomly calculated using Random(). Is there a way to do this only with XAML and the viewmodel?
Willson Haw