Account Options

  1. Sign in
The old Google Groups will be going away soon.
Switch to the new Google Groups.
Google Groups Home
« Groups Home
Message from discussion Controlling focus from ViewModel objects
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Peter O'Hanlon  
View profile  
 More options Mar 15 2010, 10:31 am
From: "Peter O'Hanlon" <pete.ohan...@gmail.com>
Date: Mon, 15 Mar 2010 14:31:01 +0000
Local: Mon, Mar 15 2010 10:31 am
Subject: Re: [WPF Disciples] Controlling focus from ViewModel objects

The only thing that stands out to me is that this is going to have to be
applied in a lot of places in a typical LOB application, so that might be a
little bit tedious. What effects do FocusScope's have here? I'll need to
play around with this a bit just to make sure there aren't any edge cases
that occur (I'm primarily thinking of cases where loss of focus to a
menu/toolbar triggers the validation but the parent FocusScope has changed
from the textbox).

Pete

On Mon, Mar 15, 2010 at 2:24 PM, Josh Smith <flappleja...@gmail.com> wrote:
> Thanks.  Any pitfalls or gotchas sticking their heads out?

> On Mon, Mar 15, 2010 at 1:03 AM, Peter O'Hanlon <pete.ohan...@gmail.com>wrote:

>> I like it - I know that it feels "dirty", but it's actually pretty darn
>> cool.

>>  On Mon, Mar 15, 2010 at 3:54 AM, Josh Smith <flappleja...@gmail.com>wrote:

>>> A while back Dr. WPF graced us with his clever hack of controlling input
>>> focus from VM objects.  It involved hijacking the VM's IDataErrorInfo
>>> implementation, and doing all sorts of evil-genius stuff to make the input
>>> caret move to the correct element.  Today while I was *trying* to read a
>>> great novel, I couldn't stop thinking about another way to control input
>>> focus from a viewmodel object.

>>> I put the novel down and did a quick spike of the idea.  The source code
>>> is attached (rename it from .DOC to .ZIP since gmail is terrified of ZIP
>>> files for some reason).  Here's the general idea...

>>> Implement this interface on your VM class:

>>>  /// <summary>
>>> /// Implemented by a ViewModel that needs to control
>>> /// where input focus is in a View.
>>> /// </summary>
>>> public interface IFocusController
>>> {
>>>     /// <summary>
>>>     /// Raised when the input focus should move to
>>>     /// a control whose 'active' dependency property
>>>     /// is bound to the specified property.
>>>     /// </summary>
>>>     event EventHandler<MoveFocusEventArgs> MoveFocus;
>>> }

>>> Next, in your VM object (which might implement IDataErrorInfo), do
>>> something like this to support a Save button:

>>>  public ICommand SaveCommand
>>> {
>>>     get { return new RelayCommand(this.Save); }
>>> }

>>> void Save()
>>> {
>>>     if (this.HasError)
>>>     {
>>>         // The Error property knows which property is invalid.
>>>         this.RaiseMoveFocus(this.Error);
>>>     }
>>>     else
>>>     {
>>>         // Save to database...
>>>         System.Windows.MessageBox.Show("Saved");
>>>     }
>>> }

>>> bool HasError
>>> {
>>>     get { return _validatedProperties.Any(prop =>
>>> !String.IsNullOrEmpty(this[prop])); }
>>> }

>>>  public event EventHandler<MoveFocusEventArgs> MoveFocus;

>>> void RaiseMoveFocus(string focusedProperty)
>>> {
>>>     var handler = this.MoveFocus;
>>>     if (handler != null)
>>>     {
>>>         handler(this, new MoveFocusEventArgs(focusedProperty));
>>>     }
>>> }

>>> Lastly, in your View, use my terribly(?) named attached property to
>>> specify which DP on your elements is bound to the validated property of the
>>> VM, for example:

>>>  <TextBox
>>>     Text="{Binding Path=FirstName, ValidatesOnDataErrors=True}"
>>>     *local:FocusControl.ValidatedProperty="TextBox.Text"*
>>>     />

>>> I'm not sure that I like this approach yet.  It's a pretty important
>>> topic, so I thought I'd share this out amongst the Disciples for feedback.

>>> Thanks,
>>> Josh

>> --
>> Peter O'Hanlon

--
Peter O'Hanlon

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.