View Logic in the Codebehind - MVVM w/ BxF

View Logic in the Codebehind - MVVM w/ BxF

Old forum URL: forums.lhotka.net/forums/t/9492.aspx


gizmo posted on Thursday, September 09, 2010

I'm implementing my first MVVM-based project using BxF and CSLA 4 (SL). I understand the importance of keeping view-model logic away from the view. I also understand it is best practice to have very little if anything in the view. Which works wonderfully if you're simply binding to properties and executing verbs on the view-model. What about "view" specific behavior?

For instance, the application is expected to place focus in the first invalid field of a form after the user attempts to submit. The view model can't and shouldn't do that work since it knows nothing about the view. The view of course can do that work; however, this means code in the codebehind and it seems to me the view would need to subscribe to some view-model event; like SubmitCompleted.

-Gizmo

RockfordLhotka replied on Thursday, September 09, 2010

Focus management is a common issue. There's an actual focus manager in WPF, but not in SL. And I've found the WPF one to be somewhat limiting.

One thing I've done is to create a UI control that sets focus on a target control. Then I use a TriggerAction to invoke the SetFocus() method on my Focus control based on an event.

Even that turns out to be somewhat challenging - even for something as simple as setting focus when a view is loaded, because of the way events work within various containers (like panels, etc).

I don't really think this should be so hard, but it seems to be...

Devaron replied on Thursday, September 16, 2010

Hi Rocky,

I'm one of the developers working with the original poster of this message.  One of the solutions we came up with is to create a TriggerAction Behavior, which we can add to Views, which automatically finds the first Control (i.e. TextBox) in the UserCotnrol, which is displaying the ValidationErrorElement Border and sets focus to the Control.  And basically we configure the TriggerAction to fire based on an Event.  This works great, but I would like to be able to bind the EventTrigger's EventName Property to an Event being raised by the ViewModel, not the Code-Behind of the View.

I've been playing around with Binding syntax, but can't seem to get the right configuration to do this. Below is one of the options. The SL Project compiles, but the Event (ValidationFailed) in the ViewModel is never handled by our Trigger.

 

<.. ViewModel setup

 

 

 

<CollectionViewSource x:Key="LoginViewModelViewSource" />

..>

 

 

 

 

<!-- Trigger Xaml -->

<

 

 

i:Interaction.Triggers>

 

 

 

 

<i:EventTrigger EventName="ValidationFailed" SourceObject="{Binding Source={StaticResource LoginViewModelViewSource}, Path=View.CurrentItem}">

<appTriggers:SetFocusFirstInvalidControlTrigger/>

 

 

 

 

</i:EventTrigger>

 

 

 

 

</i:Interaction.Triggers>

RockfordLhotka replied on Thursday, September 16, 2010

I don't know if that is possible, it might be outside the scope of EventTrigger to listen for non-UI events. It is a question you might want to ask in one of the MSDN forums.

Copyright (c) Marimer LLC