Account Options

  1. Sign in
Google Groups Home
« Groups Home
Controlling focus from ViewModel objects
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  Messages 1 - 25 of 71 - Collapse all  -  Translate all to Translated (View all originals)   Newer >
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
 
Josh Smith  
View profile   Translate to Translated (View Original)
 More options Mar 14 2010, 11:54 pm
From: Josh Smith <flappleja...@gmail.com>
Date: Sun, 14 Mar 2010 19:54:06 -0800
Local: Sun, Mar 14 2010 11:54 pm
Subject: Controlling focus from ViewModel objects

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

  WpfApplication1.zip.DOC
37K Download

 
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.
Sacha Barber  
View profile  
 More options Mar 15 2010, 4:55 am
From: Sacha Barber <sacha.bar...@gmail.com>
Date: Mon, 15 Mar 2010 08:55:03 +0000
Local: Mon, Mar 15 2010 4:55 am
Subject: Re: [WPF Disciples] Controlling focus from ViewModel objects

I like it.

--
Sacha Barber
sacha.bar...@gmail.com

 
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.
Peter O'Hanlon  
View profile  
 More options Mar 15 2010, 5:03 am
From: "Peter O'Hanlon" <pete.ohan...@gmail.com>
Date: Mon, 15 Mar 2010 09:03:37 +0000
Local: Mon, Mar 15 2010 5:03 am
Subject: Re: [WPF Disciples] Controlling focus from ViewModel objects

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

--
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.
Josh Smith  
View profile  
 More options Mar 15 2010, 10:24 am
From: Josh Smith <flappleja...@gmail.com>
Date: Mon, 15 Mar 2010 06:24:25 -0800
Local: Mon, Mar 15 2010 10:24 am
Subject: Re: [WPF Disciples] Controlling focus from ViewModel objects

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:


 
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.
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

--
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.
Josh Smith  
View profile  
 More options Mar 15 2010, 11:08 am
From: Josh Smith <flappleja...@gmail.com>
Date: Mon, 15 Mar 2010 07:08:26 -0800
Local: Mon, Mar 15 2010 11:08 am
Subject: Re: [WPF Disciples] Controlling focus from ViewModel objects

Thanks Pete. I was thinking that there could be an attached property of type
bool that is set on an element, instead of setting ValidatedProperty.  When
set, that attached property figures out which property on the element to
monitor, such as Text on a TextBox.  This would make it easier than having
to specify the property, though that would still be an option if you need to
specify it.

I'll have to look into FocusScopes, though I don't see any issues there (I
might be overlooking something...).

Josh

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


 
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.
Peter O'Hanlon  
View profile  
 More options Mar 15 2010, 11:12 am
From: "Peter O'Hanlon" <pete.ohan...@gmail.com>
Date: Mon, 15 Mar 2010 15:12:55 +0000
Local: Mon, Mar 15 2010 11:12 am
Subject: Re: [WPF Disciples] Controlling focus from ViewModel objects

Sounds good. As far as the FocusScopes go, there shouldn't be any issues,
but it's an edge case to consider (I've been bitten too many times by custom
FocusScopes).

--
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.
Marlon Grech  
View profile   Translate to Translated (View Original)
 More options Mar 15 2010, 1:46 pm
From: Marlon Grech <marlongr...@gmail.com>
Date: Mon, 15 Mar 2010 17:46:11 +0000
Local: Mon, Mar 15 2010 1:46 pm
Subject: Re: [WPF Disciples] Controlling focus from ViewModel objects

mmm... interesting approach... what about leveraging Namescopes for such a
thing?

Regards
Marlon
WPF Blog - http://marlongrech.wordpress.com/
Microsoft MVP for Client App

On Mon, Mar 15, 2010 at 3:12 PM, Peter O'Hanlon <pete.ohan...@gmail.com>wrote:


 
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.
Josh Smith  
View profile  
 More options Mar 15 2010, 1:50 pm
From: Josh Smith <flappleja...@gmail.com>
Date: Mon, 15 Mar 2010 10:50:11 -0700
Local: Mon, Mar 15 2010 1:50 pm
Subject: Re: [WPF Disciples] Controlling focus from ViewModel objects

What do you have in mind?

On Mon, Mar 15, 2010 at 10:46 AM, Marlon Grech <marlongr...@gmail.com>wrote:


 
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.
Marlon Grech  
View profile  
 More options Mar 15 2010, 2:33 pm
From: Marlon Grech <marlongr...@gmail.com>
Date: Mon, 15 Mar 2010 18:33:17 +0000
Local: Mon, Mar 15 2010 2:33 pm
Subject: Re: [WPF Disciples] Controlling focus from ViewModel objects

something like this... but I need to do something else so that its cooler...

The Idea is that rather then setting an attached property to every element
you want to control you just set it once and find that element by using WPF
Namescopes.

Attached is a prototype... yet I just built this now... did not test it...
and probably I have bugs all over the place.,... I just did it to show you
what I mean

Regards
Marlon
WPF Blog - http://marlongrech.wordpress.com/
Microsoft MVP for Client App

  TestFocus.zip.doc
140K Download

 
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.
Sacha Barber  
View profile  
 More options Mar 15 2010, 2:57 pm
From: Sacha Barber <sacha.bar...@gmail.com>
Date: Mon, 15 Mar 2010 18:57:42 +0000
Local: Mon, Mar 15 2010 2:57 pm
Subject: Re: [WPF Disciples] Controlling focus from ViewModel objects

I like it actually Marlon, very nice, though I have not had that much to do
with FocusScope to be honest, I must be doing it all wrong I guess.

--
Sacha Barber
sacha.bar...@gmail.com

 
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.
Peter O'Hanlon  
View profile  
 More options Mar 15 2010, 4:08 pm
From: "Peter O'Hanlon" <pete.ohan...@gmail.com>
Date: Mon, 15 Mar 2010 20:08:26 +0000
Local: Mon, Mar 15 2010 4:08 pm
Subject: Re: [WPF Disciples] Controlling focus from ViewModel objects

I like that Marlon. It's a neat twist, and an innovative use of namescopes.

--
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.
Marlon Grech  
View profile  
 More options Mar 15 2010, 4:22 pm
From: Marlon Grech <marlongr...@gmail.com>
Date: Mon, 15 Mar 2010 20:22:21 +0000
Local: Mon, Mar 15 2010 4:22 pm
Subject: Re: [WPF Disciples] Controlling focus from ViewModel objects

yea I think Namescope can be leveraged in this specific scenario...

there is another way of doing this which would actually be cooler because
you do not need to implement any interfaces... let me write a quick
sample.... brb

Regards
Marlon
WPF Blog - http://marlongrech.wordpress.com/
Microsoft MVP for Client App

On Mon, Mar 15, 2010 at 8:08 PM, Peter O'Hanlon <pete.ohan...@gmail.com>wrote:


 
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.
Daniel Vaughan  
View profile  
 More options Mar 15 2010, 4:25 pm
From: Daniel Vaughan <dbvaug...@gmail.com>
Date: Mon, 15 Mar 2010 13:25:54 -0700 (PDT)
Local: Mon, Mar 15 2010 4:25 pm
Subject: Re: Controlling focus from ViewModel objects
Good thinking Marlon. As Pete said, very innovative.

On Mar 15, 7:33 pm, Marlon Grech <marlongr...@gmail.com> wrote:


 
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.
Marlon Grech  
View profile  
 More options Mar 15 2010, 4:42 pm
From: Marlon Grech <marlongr...@gmail.com>
Date: Mon, 15 Mar 2010 20:42:22 +0000
Local: Mon, Mar 15 2010 4:42 pm
Subject: Re: [WPF Disciples] Re: Controlling focus from ViewModel objects

Attached is take 2...

Basically I hate it when my ViewModel has to implement all these
interfaces... It feels like back in Java days to me...

so the idea here is to still use namescopes but leverage the databinding
capabilities of WPF... so that you can do something like this

<Grid focusStuff:FocusedBehaviourTake2.FocusElement="{Binding Focus}">

and in the VM

Save = new RelayCommand( x => Focus= "Name");

Basically in the ViewModel you set a property that exposes the element that
should get focus. The attached behavior (which accepts binding) will get
triggered when the property changes. THERE IS ONE SIDE EFFECT OF THIS.
Basically if the viewmodel sets the Focus property to be the same the
attached behavior will not get triggered again... this might be a show
stopper for this idea... what do you guys think?

which approach would you like best?

Regards
Marlon
WPF Blog - http://marlongrech.wordpress.com/
Microsoft MVP for Client App

  TestFocus Take 2.zip.doc
157K Download

 
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.
Marlon Grech  
View profile  
 More options Mar 15 2010, 4:43 pm
From: Marlon Grech <marlongr...@gmail.com>
Date: Mon, 15 Mar 2010 20:43:33 +0000
Local: Mon, Mar 15 2010 4:43 pm
Subject: Re: [WPF Disciples] Re: Controlling focus from ViewModel objects

P.S sorry for ugly code and all that but the code I produced is just to show
you guys the idea ....

Regards
Marlon
WPF Blog - http://marlongrech.wordpress.com/
Microsoft MVP for Client App


 
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.
Sacha Barber  
View profile  
 More options Mar 15 2010, 5:18 pm
From: Sacha Barber <sacha.bar...@gmail.com>
Date: Mon, 15 Mar 2010 21:18:58 +0000
Local: Mon, Mar 15 2010 5:18 pm
Subject: Re: [WPF Disciples] Re: Controlling focus from ViewModel objects

I prefer option 1 actually

--
Sacha Barber
sacha.bar...@gmail.com

 
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.
Peter O'Hanlon  
View profile   Translate to Translated (View Original)
 More options Mar 15 2010, 5:20 pm
From: "Peter O'Hanlon" <pete.ohan...@gmail.com>
Date: Mon, 15 Mar 2010 21:20:43 +0000
Local: Mon, Mar 15 2010 5:20 pm
Subject: Re: [WPF Disciples] Re: Controlling focus from ViewModel objects

Same here.

On Mon, Mar 15, 2010 at 9:18 PM, Sacha Barber <sacha.bar...@gmail.com>wrote:

--
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.
Marlon Grech  
View profile   Translate to Translated (View Original)
 More options Mar 15 2010, 5:56 pm
From: Marlon Grech <marlongr...@gmail.com>
Date: Mon, 15 Mar 2010 21:56:09 +0000
Local: Mon, Mar 15 2010 5:56 pm
Subject: Re: [WPF Disciples] Re: Controlling focus from ViewModel objects

may I ask why you guys prefer option 1? Just for curiosity...
Regards
Marlon
WPF Blog - http://marlongrech.wordpress.com/
Microsoft MVP for Client App

On Mon, Mar 15, 2010 at 9:20 PM, Peter O'Hanlon <pete.ohan...@gmail.com>wrote:


 
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.
Sacha Barber  
View profile   Translate to Translated (View Original)
 More options Mar 15 2010, 6:04 pm
From: Sacha Barber <sacha.bar...@gmail.com>
Date: Mon, 15 Mar 2010 22:04:24 +0000
Local: Mon, Mar 15 2010 6:04 pm
Subject: Re: [WPF Disciples] Re: Controlling focus from ViewModel objects

I do not like the idea of the Binding not firing.

...

read more »


 
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.
Marlon Grech  
View profile  
 More options Mar 15 2010, 6:06 pm
From: Marlon Grech <marlongr...@gmail.com>
Date: Mon, 15 Mar 2010 22:06:10 +0000
Local: Mon, Mar 15 2010 6:06 pm
Subject: Re: [WPF Disciples] Re: Controlling focus from ViewModel objects

true ... it is a show stopper isn't it...

Regards
Marlon
WPF Blog - http://marlongrech.wordpress.com/
Microsoft MVP for Client App

On Mon, Mar 15, 2010 at 10:04 PM, Sacha Barber <sacha.bar...@gmail.com>wrote:

...

read more »


 
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.
Peter O'Hanlon  
View profile  
 More options Mar 15 2010, 6:06 pm
From: "Peter O'Hanlon" <pete.ohan...@gmail.com>
Date: Mon, 15 Mar 2010 22:06:14 +0000
Local: Mon, Mar 15 2010 6:06 pm
Subject: Re: [WPF Disciples] Re: Controlling focus from ViewModel objects

It just feels cleaner to me (plus the namescope way is way cool).

...

read more »


 
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.
Marlon Grech  
View profile  
 More options Mar 15 2010, 6:08 pm
From: Marlon Grech <marlongr...@gmail.com>
Date: Mon, 15 Mar 2010 22:08:04 +0000
Local: Mon, Mar 15 2010 6:08 pm
Subject: Re: [WPF Disciples] Re: Controlling focus from ViewModel objects

actually there is a way how you can work around that... but I am not gonna
do that... the other way is to inject a command in the ViewModel and the
ViewModel can then execute that command...

actually you know what... I am at a pub and have nothing to do (besides
drinking beer) so I will have a go with that Idea :) lol

Regards
Marlon
WPF Blog - http://marlongrech.wordpress.com/
Microsoft MVP for Client App

On Mon, Mar 15, 2010 at 10:06 PM, Marlon Grech <marlongr...@gmail.com>wrote:

...

read more »


 
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.
Marlon Grech  
View profile  
 More options Mar 15 2010, 6:50 pm
From: Marlon Grech <marlongr...@gmail.com>
Date: Mon, 15 Mar 2010 22:50:31 +0000
Local: Mon, Mar 15 2010 6:50 pm
Subject: Re: [WPF Disciples] Re: Controlling focus from ViewModel objects

here is take 3 :)

P.S I am on my second Pint so excuse any stupid mistakes :D

The idea is that the Attached behavior injects a command that the ViewModel
can use to get an element focused. (it is still using the namescope idea).

This is what the ViewModel does
ICommand focusCommand;
        public ICommand FocusCommand
        {
            get { return focusCommand; }
            set
            {
                focusCommand = value;
                OnPropertyChanged("FocusCommand");
            }
        }

        public ICommand Save { get; private set; }

        public MyViewModel()
        {
            Save = new RelayCommand(x =>
            {
                if (FocusCommand.CanExecute("Name"))
                    FocusCommand.Execute("Name");
             }

So the ViewModel exposes a command property but never sets it.

Then in the View you work a bit of magic
<Grid focusStuff:FocusedBehaviourTake3.HandleFocusElement="True"
focusStuff:FocusedBehaviourTake3.FocusCommand="{Binding FocusCommand,
Mode=TwoWay}" >

Basically the Behaviour will inject a command that the ViewModel will
execute. This option gives also the oportunity to check if you can actually
give focus to the control which is very interesting from the ViewModel stand
point. What do you think?

Regards
Marlon
WPF Blog - http://marlongrech.wordpress.com/
Microsoft MVP for Client App

On Mon, Mar 15, 2010 at 10:08 PM, Marlon Grech <marlongr...@gmail.com>wrote:

...

read more »

  TestFocus Take 3.zip.doc
165K Download

 
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.
Marlon Grech  
View profile  
 More options Mar 15 2010, 7:22 pm
From: Marlon Grech <marlongr...@gmail.com>
Date: Mon, 15 Mar 2010 23:22:58 +0000
Local: Mon, Mar 15 2010 7:22 pm
Subject: Re: [WPF Disciples] Re: Controlling focus from ViewModel objects

to make this less magic one could create an interface and make the Attached
Behaviour set a property on that interface.... yet having said that then why
not go to Take 1.... mmmm.... aa well... its good to have option now all you
need to do is pick one :)

But yea I think the first option is probably ideal.. the other options are
there because WPF is awesome and you can do crazy shit.... oww my beer is
talking now.. :)

Regards
Marlon
WPF Blog - http://marlongrech.wordpress.com/
Microsoft MVP for Client App

On Mon, Mar 15, 2010 at 10:50 PM, Marlon Grech <marlongr...@gmail.com>wrote: