So I always praise this declarative development that Enterprise Core Objects lends itself to. Take the ViewModels – I go on and on, post after post, rambling on how amazingly fast and robust a UI built this way can be delivered.
But what if I want something really special in my UI – something that the ViewModel engine does not support. If I have do abandon the technique then I might as well skip it in the first place some might argue. Point taken. This post is for you.
Content Override
In the ViewModel – editor above I have marked a little checkbox called “Content override” with red. And I also marked how it is rendered in the preview – like a square that says “OVERRIDE”. This is one way to get “odd” things into the otherwise purely declaratively defined UI.
When a ViewModel column has the “Content Override” set, an event will fire on the ViewModelUserControl (same for Silverlight, WPF, ASP.NET and windows forms), and in this event you can pretty much do want you want with the data provided for that column.
In code it can look something like this:
- // Set up the event handler
- EATree_Step_UC.SetEcoSpace(es);
- EATree_Step_UC.OnColumnUIOverride +=
- new EventHandler<OnColumnUIOverrideArgs>(EATree_Step_UC_OnColumnUIOverride);
- // Implement the event
- void EATree_Step_UC_OnColumnUIOverride(object sender, OnColumnUIOverrideArgs e)
- {
- if (e.ViewModelColumn.ContentOverride)
- {
- Grid g=e.ViewModelColumn.SpawnedArtifacts["control"] as Grid;
- if (e.ViewModelColumn.Name=="CompleteStepDraw")
- {
- g.DataContextChanged += new DependencyPropertyChangedEventHandler
- (g_DataContextChanged_CompleteStepDraw);
- g_DataContextChanged_CompleteStepDraw(g,
- new DependencyPropertyChangedEventArgs());// initial init
- }
- }
- }
Note the that Override area is implemented as a Grid in WPF and Silverlight and you can add the things you want to show up here with another user control or what have you. The result of the expression for the column will be in DataContext for that very Grid.
In my case it turned out like this:
Fast, flexible and low maintenance for standard UI – what is not to like?