the objective – you got items control – more specific you have got comboBox, and for each item in the collection the meaning is to Invoke some Command Action , now where is the problem ? when you select any item from the source , and at runtime like to invoke the same specific function e.g. the same selected item, the Control by default, doesnt refresh the and raise the selection change Event and when you depend on this change , your action just not showing up, and it is very simple why.. because the SelectedItem State just stay the same item and using MVVM it remaining the ViewModel side in darkness. yes you can probably solve it using codeBehind, but in several cases <depend on your code complex and what the SelectedItem-selection Change acting for > but its ugly.. and as we says its surly unsafe because you can clear the selected item before the BL part use it at all .
better solution could be using WPF Trigger and command Action with command Parameter the commanchingd parameter will be the ItemsControl itself , than in your viewModel side when finished the action than refresh the Combobox/ListBox.
Many WPF developers have found themselves against MVVM architecture in case like this. in basic it sound simple , but now its not about MVVM its about WPF controls behaviors.
The point is the MVVM make the challenge more fun.
For testing things lets start with next code:
1. Label for shoing selected color
2. comboBox for switching selected Color
3. button in position of interrupter player.
The application purpose is to switch the label color from green to red as the selected value of comboBox.
That Xaml conceted to the follow ViewModel Code:
It will be simple the figure it out when the last selection was green, than the interrupter set for something else, and when we get back to the combo and choose for green once again, it will not responds! It will reacting for selecting red! So why ?? well it is simple actually.
Since every items control has internal property called selectedItem, and the selected Item stay as it was because the label got is value from outside, and if we select same as last selected item it not called to observers and raise the SelectionChange Event ,
So, if we understand the problem we probably think about refresh the selected item after every selectionChanged, to do this using MVVM lets try use Triggers Action of System windows Interactivity.
Before we write the ‘Good’ solution lets check the bad one.
We can try this,
Well , first its breaking the MVVM for my taste, but worse it could be act before the command invokes or better to say you will never know !!
So the better option will be the next.
Add reference to system windows interactivity,
So first change the xaml as follow :
And your c# code will look like that: