Ok, this is a little hard for me to explain, so Im writing a program using MVVM model with WPF.
I have a grid on my main UserControl which requires the children to be updated (by updated I mean created and destroyed) dynamically through code (as data is changed by the ViewModel I want the children of the Grid to be created and destroyed as needed).
Im pretty sure you cant dynamically create and destroy UIElements using only XAML, so I have update functions in my view (these update functions ONLY update the UI itself they do not change anything in the ViewModel, so therefore im not breaking MVVM because the code in the view only alters the UI based on the data in the ViewModel, so thats ok right?).
Right now I am assigning the PropertyChanged event of my ViewModel within the View and catching that through code and firing the Update events, so my event in my View class looks like this:
private void ViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName == "BoardWidth" || e.PropertyName == "BoardHeight")
RecreateBoard();
if (e.PropertyName == "Coordinates")
UpdateCoordinates();
if (e.PropertyName == "TilesOnly")
{
UpdateBoardBlocks();
UpdateTiles();
}
if (e.PropertyName == "BoardBlockViewModels")
UpdateBoardBlocks();
if (e.PropertyName == "TileViewModels")
UpdateTiles();
if (e.PropertyName == "EntryPoint" | e.PropertyName == "CursorViewModel")
UpdateEntryPoint();
}
The update events create / destroy children within the main grid of the view using code based on the data in the ViewModel. This current implementation works well.
But, I would like to implement the event handler through XAML. So my question is how do I implement the event handler code above in XAML, I cant find any way of executing a Method within the view when a datacontext property changes. I have looked into DataTriggers but they dont contain the ability to execute the methods. Also the DataTrigger would need to fire when the property changes, not when it is set to a specific value.
I do not wish to push the updates through the ViewModel because this is STICTLY a View thing it has nothing to do with the viewmodel itself. eg. A different implementation of the View may not need to update in these circumstances. Even if I was to pus it through the ViewModel, the view itself would still have the same issue of how to execute the methods.
Thankyou for your help.
I think the way you approach may be wrong if you need that much imperative code in your view. In most cases you just bind data to lists and fields etc. You rarely need to create and remove elements, you either have controls autogenerate them (e.g.
ItemsControls) or you reuse the existing instances.If you do need to do such dynamic things it might be best to encapsulate that logic in
UserControlsor custom controls, just offering properties that can be bound (internally you can handle the dependency property’s value changed event to do any updates).What is left in the view should be bindings to those controls.