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
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 26 - 50 of 71 - Collapse all  -  Translate all to Translated (View all originals) < Older  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  
 More options Mar 15 2010, 10:33 pm
From: Josh Smith <flappleja...@gmail.com>
Date: Mon, 15 Mar 2010 18:33:38 -0800
Local: Mon, Mar 15 2010 10:33 pm
Subject: Re: [WPF Disciples] Re: Controlling focus from ViewModel objects

I finally got some time to look over your code samples, Marlon.  It's
definitely a cool idea, and shows some creative thinking.

With that said, I wouldn't use it.  I try to avoid naming elements as much
as possible, and this technique relies on assigning names to elements.  Not
only that, but the name of an element must match the name of the VM property
to which it's "important" property is bound.  I can see this leading to
refactoring problems, learnability degradation, and general maintainability
issues.  It's too magical, you f'ing Wizard you! ;-)

The reason I used the approach shown in my original code spike is threefold.

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

Fold #1 - By specifying the dependency property to which the validated VM
property is bound, I avoid unnecessary duplication of VM property names in
the View.

Fold #2 - There is no need to walk down the element tree, searching for
elements that match a criterion.  This improves performance, especially in
obese UIs.

Fold #3 - By making the 'ValidatedProperty' attached DP of type
DependencyProperty, I get compile-time name verification.

I'm pondering the idea of creating a Binding subclass that taps into some
kind of scoped focus management container upon creation.  You could then use
that Binding extension to have the binding automatically register itself
with the entity responsible for satisfying focus change requests from VM.

Tally ho!
Josh

...

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

I took a stab at the custom binding approach that I mentioned earlier.  The
new code is attached.  Here's all that's required in the XAML to make an
element be a target for focus movement.

<TextBox
    Text="{local:FocusBinding Binding={Binding Path=FirstName,
ValidatesOnDataErrors=True}}"
    />

Technically, the ValidatesOnDataErrors isn't needed.

Here's how FocusBinding works:

public class FocusBinding : MarkupExtension
{
    public FocusBinding()
    {
    }

    public Binding Binding { get; set; }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        var provideValueTarget =
serviceProvider.GetService(typeof(IProvideValueTarget)) as
IProvideValueTarget;
        if (provideValueTarget != null)
        {
            var element = provideValueTarget.TargetObject as
DependencyObject;
            var property = provideValueTarget.TargetProperty as
DependencyProperty;
            FocusControl.SetFocusableProperty(element, property);
        }
        return this.Binding.ProvideValue(serviceProvider);
    }

}

If BindingBase.ProvideValue wasn't sealed, I could have just overridden that
method instead of creating a wrapper markup extension.  Oh well.

Josh

...

read more »

  WpfApplication1 TAKE 2.zip.DOC
38K 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.
Josh Smith  
View profile  
 More options Mar 15 2010, 11:40 pm
From: Josh Smith <flappleja...@gmail.com>
Date: Mon, 15 Mar 2010 19:40:55 -0800
Local: Mon, Mar 15 2010 11:40 pm
Subject: Re: [WPF Disciples] Re: Controlling focus from ViewModel objects

On second thought, ProvideValue should check if the Binding property is
null, and act like a {Binding} if it is:

public override object ProvideValue(IServiceProvider serviceProvider)
{
    var provideValueTarget =
serviceProvider.GetService(typeof(IProvideValueTarget)) as
IProvideValueTarget;
    if (provideValueTarget != null)
    {
        var element = provideValueTarget.TargetObject as DependencyObject;
        var property = provideValueTarget.TargetProperty as
DependencyProperty;
        FocusControl.SetFocusableProperty(element, property);
    }

    *if (this.Binding == null)*
*        this.Binding = new Binding();*

    return this.Binding.ProvideValue(serviceProvider);

...

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.
Sacha Barber  
View profile  
 More options Mar 16 2010, 5:05 am
From: Sacha Barber <sacha.bar...@gmail.com>
Date: Tue, 16 Mar 2010 09:05:22 +0000
Local: Tues, Mar 16 2010 5:05 am
Subject: Re: [WPF Disciples] Re: Controlling focus from ViewModel objects

I like this last one from Josh a lot too. Very elegant both Josh / Marlon

...

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.
Philipp Sumi  
View profile  
 More options Mar 16 2010, 5:23 am
From: "Philipp Sumi" <phil...@hardcodet.net>
Date: Tue, 16 Mar 2010 10:23:33 +0100
Local: Tues, Mar 16 2010 5:23 am
Subject: RE: [WPF Disciples] Re: Controlling focus from ViewModel objects

Very nice approach, Josh - markup extensions are one of the really underestimated tools in a WPF developer's toolbox. In case you want to save yourself the plumbing of all the binding properties, you can take the source of this decorator class here - I'm using that one quite often for similar scenarios, but encapsulates the whole binding stuff in a base class: http://www.hardcodet.net/2008/04/wpf-custom-binding-class

However, maybe one could solve the problem more designer (less XAML) friendly using Blend behaviors? I gonna have to check that out.

From: wpf-disciples@googlegroups.com [mailto:wpf-disciples@googlegroups.com] On Behalf Of Josh Smith
Sent: Dienstag, 16. März 2010 04:41
To: wpf-disciples@googlegroups.com
Subject: Re: [WPF Disciples] Re: Controlling focus from ViewModel objects

On second thought, ProvideValue should check if the Binding property is null, and act like a {Binding} if it is:

public override object ProvideValue(IServiceProvider serviceProvider)
{
    var provideValueTarget = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
    if (provideValueTarget != null)
    {
        var element = provideValueTarget.TargetObject as DependencyObject;
        var property = provideValueTarget.TargetProperty as DependencyProperty;
        FocusControl.SetFocusableProperty(element, property);
    }

    if (this.Binding == null)
        this.Binding = new Binding();

    return this.Binding.ProvideValue(serviceProvider);

}
On Mon, Mar 15, 2010 at 7:38 PM, Josh Smith <flappleja...@gmail.com> wrote:

I took a stab at the custom binding approach that I mentioned earlier.  The new code is attached.  Here's all that's required in the XAML to make an element be a target for focus movement.

<TextBox
    Text="{local:FocusBinding Binding={Binding Path=FirstName, ValidatesOnDataErrors=True}}"
    />

Technically, the ValidatesOnDataErrors isn't needed.  

Here's how FocusBinding works:

public class FocusBinding : MarkupExtension
{
    public FocusBinding()
    {
    }

    public Binding Binding { get; set; }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        var provideValueTarget = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
        if (provideValueTarget != null)
        {
            var element = provideValueTarget.TargetObject as DependencyObject;
            var property = provideValueTarget.TargetProperty as DependencyProperty;
            FocusControl.SetFocusableProperty(element, property);
        }
        return this.Binding.ProvideValue(serviceProvider);
    }

}

If BindingBase.ProvideValue wasn't sealed, I could have just overridden that method instead of creating a wrapper markup extension.  Oh well.

Josh

On Mon, Mar 15, 2010 at 6:33 PM, Josh Smith <flappleja...@gmail.com> wrote:

I finally got some time to look over your code samples, Marlon.  It's definitely a cool idea, and shows some creative thinking.  

With that said, I wouldn't use it.  I try to avoid naming elements as much as possible, and this technique relies on assigning names to elements.  Not only that, but the name of an element must match the name of the VM property to which it's "important" property is bound.  I can see this leading to refactoring problems, learnability degradation, and general maintainability issues.  It's too magical, you f'ing Wizard you! ;-)

The reason I used the approach shown in my original code spike is threefold.

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

Fold #1 - By specifying the dependency property to which the validated VM property is bound, I avoid unnecessary duplication of VM property names in the View.

Fold #2 - There is no need to walk down the element tree, searching for elements that match a criterion.  This improves performance, especially in obese UIs.

Fold #3 - By making the 'ValidatedProperty' attached DP of type DependencyProperty, I get compile-time name verification.

I'm pondering the idea of creating a Binding subclass that taps into some kind of scoped focus management container upon creation.  You could then use that Binding extension to have the binding automatically register itself with the entity responsible for satisfying focus change requests from VM.

Tally ho!
Josh

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

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:

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:

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:

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:

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

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

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:

Same here.

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

I prefer option 1 actually

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

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

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

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

On Mon, Mar 15, 2010 at 8:25 PM, Daniel Vaughan <dbvaug...@gmail.com> wrote:

Good thinking Marlon. As Pete said, very innovative.

On Mar 15, 7:33 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.
Philipp Sumi  
View profile  
 More options Mar 16 2010, 7:58 am
From: "Philipp Sumi" <phil...@hardcodet.net>
Date: Tue, 16 Mar 2010 12:58:30 +0100
Local: Tues, Mar 16 2010 7:58 am
Subject: RE: [WPF Disciples] Re: Controlling focus from ViewModel objects

Ok, I did a quick dive into Blend behaviors and came up with an alternative (but not necessarily better) approach.
I put the sample here, Google didn't like my attachment despite the changed extension: http://www.hardcodet.net/uploads/focus-behaviors.zip

What's nice about the binding is that is creates the relation between the focusing and a bound property. With a behavior, we would have to define the bound dependency property ourselves. Accordingly, I came up with (or rather reused, I'm currently on something related) an abstract behavior class that delegates the resolution of the bound property to the implementations:

public abstract class FocusBehaviorBase : Behavior<FrameworkElement>
{
  protected abstract DependencyProperty GetSourceProperty();

  protected override void OnAttached()
  {
    ...
  }

}

With this behavior in place, I created two behavior classes - one that can be used specifically for TextBoxes, and a generic one that expects me to declare the bound dependency property within Blend:

public class TextBoxFocusBehavior : FocusBehaviorBase
{
  protected override DependencyProperty GetSourceProperty()
  {
    return TextBox.TextProperty;
  }

}

public class CustomFocusBehavior : FocusBehaviorBase
{
  public static readonly DependencyProperty BoundSourceProperty =
    DependencyProperty.Register("BoundSource", typeof (DependencyProperty), typeof (CustomFocusBehavior));

  public DependencyProperty BoundSource
  {
    get { return (DependencyProperty)GetValue(BoundSourceProperty); }
    set { SetValue(BoundSourceProperty, value); }
  }

  protected override DependencyProperty GetSourceProperty()
  {
    return BoundSource;
  }

}

My current implementation does not leverage any further possibilities I have with behaviors, but sticks close to Josh's implementation, using the FocusControl helper class. Here's my minimal implementation within the behavior base class (without cleanup etc):

public abstract class FocusBehaviorBase : Behavior<FrameworkElement>
{
  protected abstract DependencyProperty GetSourceProperty();

  protected override void OnAttached()
  {
    var focusController = AssociatedObject.DataContext as IFocusController;
    if(focusController == null) return;

    var elem = AssociatedObject as UIElement;
    var handler = new FocusControl.FocusControlHandler(elem, GetSourceProperty());
    focusController.MoveFocus += handler.HandleMoveFocus;
  }

}

This already covers the scenarios in the sample application, allowing you to use regular bindings, and simply drop a behavior on the controls you want to switch focus. Certainly nice from a designer's point of view :)

From: wpf-disciples@googlegroups.com [mailto:wpf-disciples@googlegroups.com] On Behalf Of Philipp Sumi
Sent: Dienstag, 16. März 2010 10:24
To: wpf-disciples@googlegroups.com
Subject: RE: [WPF Disciples] Re: Controlling focus from ViewModel objects

Very nice approach, Josh - markup extensions are one of the really underestimated tools in a WPF developer's toolbox. In case you want to save yourself the plumbing of all the binding properties, you can take the source of this decorator class here - I'm using that one quite often for similar scenarios, but encapsulates the whole binding stuff in a base class: http://www.hardcodet.net/2008/04/wpf-custom-binding-class

However, maybe one could solve the problem more designer (less XAML) friendly using Blend behaviors? I gonna have to check that out.

From: wpf-disciples@googlegroups.com [mailto:wpf-disciples@googlegroups.com] On Behalf Of Josh Smith
Sent: Dienstag, 16. März 2010 04:41
To: wpf-disciples@googlegroups.com
Subject: Re: [WPF Disciples] Re: Controlling focus from ViewModel objects

On second thought, ProvideValue should check if the Binding property is null, and act like a {Binding} if it is:

public override object ProvideValue(IServiceProvider serviceProvider)
{
    var provideValueTarget = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
    if (provideValueTarget != null)
    {
        var element = provideValueTarget.TargetObject as DependencyObject;
        var property = provideValueTarget.TargetProperty as DependencyProperty;
        FocusControl.SetFocusableProperty(element, property);
    }

    if (this.Binding == null)
        this.Binding = new Binding();

    return this.Binding.ProvideValue(serviceProvider);

}
On Mon, Mar 15, 2010 at 7:38 PM, Josh Smith <flappleja...@gmail.com> wrote:

I took a stab at the custom binding approach that I mentioned earlier.  The new code is attached.  Here's all that's required in the XAML to make an element be a target for focus movement.

<TextBox
    Text="{local:FocusBinding Binding={Binding Path=FirstName, ValidatesOnDataErrors=True}}"
    />

Technically, the ValidatesOnDataErrors isn't needed.  

Here's how FocusBinding works:

public class FocusBinding : MarkupExtension
{
    public FocusBinding()
    {
    }

    public Binding Binding { get; set; }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        var provideValueTarget = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
        if (provideValueTarget != null)
        {
            var element = provideValueTarget.TargetObject as DependencyObject;
            var property = provideValueTarget.TargetProperty as DependencyProperty;
            FocusControl.SetFocusableProperty(element, property);
        }
        return this.Binding.ProvideValue(serviceProvider);
    }

}

If BindingBase.ProvideValue wasn't sealed, I could have just overridden that method instead of creating a wrapper markup extension.  Oh well.

Josh

On Mon, Mar 15, 2010 at 6:33 PM, Josh Smith <flappleja...@gmail.com> wrote:

I finally got some time to look over your code samples, Marlon.  It's definitely a cool idea, and shows some creative thinking.  

With that said, I wouldn't use it.  I try to avoid naming elements as much as possible, and this technique relies on assigning names to elements.  Not only that, but the name of an element must match the name of the VM property to which it's "important" property is bound.  I can see this leading to refactoring problems, learnability degradation, and general maintainability issues.  It's too magical, you f'ing Wizard you! ;-)

The reason I used the approach shown in my original code spike is threefold.

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

Fold #1 - By specifying the dependency property to which the validated VM property is bound, I avoid unnecessary duplication of VM property names in the View.

Fold #2 - There is no need to walk down the element tree, searching for elements that match a criterion.  This improves performance, especially in obese UIs.

Fold #3 - By making the 'ValidatedProperty' attached DP of type DependencyProperty, I get compile-time name verification.

I'm pondering the idea of creating a Binding subclass that taps into some kind of scoped focus management container upon creation.  You could then use that Binding extension to have the binding automatically register itself with the entity responsible for satisfying focus change requests from VM.

Tally ho!
Josh

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

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:

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:

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

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 16 2010, 8:04 am
From: "Peter O'Hanlon" <pete.ohan...@gmail.com>
Date: Tue, 16 Mar 2010 12:04:09 +0000
Local: Tues, Mar 16 2010 8:04 am
Subject: Re: [WPF Disciples] Re: Controlling focus from ViewModel objects

Ooohhhh. Very nice, very nice indeed. I really need to get more into Blend
behaviours.

On Tue, Mar 16, 2010 at 11:58 AM, Philipp Sumi <phil...@hardcodet.net>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.
Bill Kempf  
View profile  
 More options Mar 16 2010, 8:13 am
From: Bill Kempf <weke...@gmail.com>
Date: Tue, 16 Mar 2010 07:13:13 -0500
Local: Tues, Mar 16 2010 8:13 am
Subject: Re: [WPF Disciples] Re: Controlling focus from ViewModel objects
I don't have time to go over the code in depth to understand if
there's something new to leverage here (Josh's comment about not
wanting to use the Name property makes me suspect there may be some
magic here that I can leverage), but I must say that this is one
scenario where I really like Onyx's concept of using services. There's
no need for the XAML to be marked up with attached properties or
Behaviors. Onyx already has this with the IFocusSuggested service.

...

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.
Sacha Barber  
View profile  
 More options Mar 16 2010, 8:15 am
From: Sacha Barber <sacha.bar...@gmail.com>
Date: Tue, 16 Mar 2010 12:15:26 +0000
Local: Tues, Mar 16 2010 8:15 am
Subject: Re: [WPF Disciples] Re: Controlling focus from ViewModel objects

Yep like it.

On Tue, Mar 16, 2010 at 12:04 PM, Peter O'Hanlon <pete.ohan...@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 16 2010, 9:34 am
From: Marlon Grech <marlongr...@gmail.com>
Date: Tue, 16 Mar 2010 13:34:57 +0000
Local: Tues, Mar 16 2010 9:34 am
Subject: Re: [WPF Disciples] Re: Controlling focus from ViewModel objects

Interesting and very cool way of using markup extension indeed.... But
wouldn't this make it that you can only set the focus when there is an
error?
Regards
Marlon
WPF Blog - http://marlongrech.wordpress.com/
Microsoft MVP for Client App

...

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.
Sacha Barber  
View profile  
 More options Mar 16 2010, 9:38 am
From: Sacha Barber <sacha.bar...@gmail.com>
Date: Tue, 16 Mar 2010 13:38:54 +0000
Local: Tues, Mar 16 2010 9:38 am
Subject: Re: [WPF Disciples] Re: Controlling focus from ViewModel objects

I think that is what Joshs original code did matey!!!

...

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   Translate to Translated (View Original)
 More options Mar 16 2010, 9:53 am
From: Marlon Grech <marlongr...@gmail.com>
Date: Tue, 16 Mar 2010 13:53:51 +0000
Local: Tues, Mar 16 2010 9:53 am
Subject: Re: [WPF Disciples] Re: Controlling focus from ViewModel objects

true but sometimes you want more then that right...

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

On Tue, Mar 16, 2010 at 1:38 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.
Zhou Yong  
View profile  
 More options Mar 16 2010, 9:57 am
From: Zhou Yong <football...@gmail.com>
Date: Tue, 16 Mar 2010 21:57:56 +0800
Local: Tues, Mar 16 2010 9:57 am
Subject: Re: [WPF Disciples] Re: Controlling focus from ViewModel objects

This is one typical scenario in which I think pure MVVM approach will not
make a good solution, MVVM + MVP like Onyx is a more natural approach to
take, and you could even easily mock up the focus management when unit
testing the VM.

Thumbs up for Bill's work

MarocZ


 
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 16 2010, 9:58 am
From: Josh Smith <flappleja...@gmail.com>
Date: Tue, 16 Mar 2010 05:58:11 -0800
Local: Tues, Mar 16 2010 9:58 am
Subject: Re: [WPF Disciples] Re: Controlling focus from ViewModel objects

Slick!  I wonder if offering a custom binding and blend behaviors is the
right way to go...

...

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.
Josh Smith  
View profile  
 More options Mar 16 2010, 9:59 am
From: Josh Smith <flappleja...@gmail.com>
Date: Tue, 16 Mar 2010 05:59:43 -0800
Local: Tues, Mar 16 2010 9:59 am
Subject: Re: [WPF Disciples] Re: Controlling focus from ViewModel objects

No, it's not restricted to when there's an error.  The demo app happens to
use this focus management stuff to move focus to an invalid control, but
that's not a requirement.

...

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

Philipp, that binding decorator class is exactly what I need.  Thanks!

...

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.
Josh Smith  
View profile   Translate to Translated (View Original)
 More options Mar 16 2010, 11:24 am
From: Josh Smith <flappleja...@gmail.com>
Date: Tue, 16 Mar 2010 07:24:05 -0800
Local: Tues, Mar 16 2010 11:24 am
Subject: Re: [WPF Disciples] Re: Controlling focus from ViewModel objects

I've blogged about the custom binding approach, and gave proper credit to
the Disciples. :)

http://joshsmithonwpf.wordpress.com/2010/03/16/control-input-focus-fr...

<http://joshsmithonwpf.wordpress.com/2010/03/16/control-input-focus-fr...>
Josh

...

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 16 2010, 11:40 am
From: "Peter O'Hanlon" <pete.ohan...@gmail.com>
Date: Tue, 16 Mar 2010 15:40:18 +0000
Local: Tues, Mar 16 2010 11:40 am
Subject: Re: [WPF Disciples] Re: Controlling focus from ViewModel objects

Good stuff Josh. BTW, you can make your codesnippets look more codey by
wrapping them in [sourcecode language='csharp'][/sourcecode] tags for C# and
[sourcecode language='xml'][/sourcecode] for XAML.

...

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

I just tried it, but WordPress kept stripping those tags out. This is why I
normally use screenshots of code, instead of plaintext.  :(

On Tue, Mar 16, 2010 at 7:40 AM, Peter O'Hanlon <pete.ohan...@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.
Sacha Barber  
View profile  
 More options Mar 16 2010, 12:11 pm
From: Sacha Barber <sacha.bar...@gmail.com>
Date: Tue, 16 Mar 2010 16:11:10 +0000
Local: Tues, Mar 16 2010 12:11 pm
Subject: Re: [WPF Disciples] Re: Controlling focus from ViewModel objects

Have I have tried that too and wordpress strips it for me too.

...

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.
Philipp Sumi  
View profile  
 More options Mar 16 2010, 12:15 pm
From: "Philipp Sumi" <phil...@hardcodet.net>
Date: Tue, 16 Mar 2010 17:15:59 +0100
Local: Tues, Mar 16 2010 12:15 pm
Subject: RE: [WPF Disciples] Re: Controlling focus from ViewModel objects

Josh,

Glad you could use it the helper class - cheers on the article :)
I was thinking about quickly publishing the alternative behavior approach, focusing on the behaviors as an alternative. Would that be okay for you if I just published it as an derivative of your legwork (including your VM and helper classes)?

Cheers,
Philipp

From: wpf-disciples@googlegroups.com [mailto:wpf-disciples@googlegroups.com] On Behalf Of Josh Smith
Sent: Dienstag, 16. März 2010 15:02
To: wpf-disciples@googlegroups.com
Subject: Re: [WPF Disciples] Re: Controlling focus from ViewModel objects

Philipp, that binding decorator class is exactly what I need.  Thanks!

On Tue, Mar 16, 2010 at 1:23 AM, Philipp Sumi <phil...@hardcodet.net> wrote:

Very nice approach, Josh - markup extensions are one of the really underestimated tools in a WPF developer's toolbox. In case you want to save yourself the plumbing of all the binding properties, you can take the source of this decorator class here - I'm using that one quite often for similar scenarios, but encapsulates the whole binding stuff in a base class: http://www.hardcodet.net/2008/04/wpf-custom-binding-class

However, maybe one could solve the problem more designer (less XAML) friendly using Blend behaviors? I gonna have to check that out.

From: wpf-disciples@googlegroups.com [mailto:wpf-disciples@googlegroups.com] On Behalf Of Josh Smith
Sent: Dienstag, 16. März 2010 04:41
To: wpf-disciples@googlegroups.com
Subject: Re: [WPF Disciples] Re: Controlling focus from ViewModel objects

On second thought, ProvideValue should check if the Binding property is null, and act like a {Binding} if it is:

public override object ProvideValue(IServiceProvider serviceProvider)
{
    var provideValueTarget = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
    if (provideValueTarget != null)
    {
        var element = provideValueTarget.TargetObject as DependencyObject;
        var property = provideValueTarget.TargetProperty as DependencyProperty;
        FocusControl.SetFocusableProperty(element, property);
    }

    if (this.Binding == null)
        this.Binding = new Binding();

    return this.Binding.ProvideValue(serviceProvider);

}
On Mon, Mar 15, 2010 at 7:38 PM, Josh Smith <flappleja...@gmail.com> wrote:

I took a stab at the custom binding approach that I mentioned earlier.  The new code is attached.  Here's all that's required in the XAML to make an element be a target for focus movement.

<TextBox
    Text="{local:FocusBinding Binding={Binding Path=FirstName, ValidatesOnDataErrors=True}}"
    />

Technically, the ValidatesOnDataErrors isn't needed.  

Here's how FocusBinding works:

public class FocusBinding : MarkupExtension
{
    public FocusBinding()
    {
    }

    public Binding Binding { get; set; }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        var provideValueTarget = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
        if (provideValueTarget != null)
        {
            var element = provideValueTarget.TargetObject as DependencyObject;
            var property = provideValueTarget.TargetProperty as DependencyProperty;
            FocusControl.SetFocusableProperty(element, property);
        }
        return this.Binding.ProvideValue(serviceProvider);
    }

}

If BindingBase.ProvideValue wasn't sealed, I could have just overridden that method instead of creating a wrapper markup extension.  Oh well.

Josh

On Mon, Mar 15, 2010 at 6:33 PM, Josh Smith <flappleja...@gmail.com> wrote:

I finally got some time to look over your code samples, Marlon.  It's definitely a cool idea, and shows some creative thinking.  

With that said, I wouldn't use it.  I try to avoid naming elements as much as possible, and this technique relies on assigning names to elements.  Not only that, but the name of an element must match the name of the VM property to which it's "important" property is bound.  I can see this leading to refactoring problems, learnability degradation, and general maintainability issues.  It's too magical, you f'ing Wizard you! ;-)

The reason I used the approach shown in my original code spike is threefold.

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

Fold #1 - By specifying the dependency property to which the validated VM property is bound, I avoid unnecessary duplication of VM property names in the View.

Fold #2 - There is no need to walk down the element tree, searching for elements that match a criterion.  This improves performance, especially in obese UIs.

Fold #3 - By making the 'ValidatedProperty' attached DP of type DependencyProperty, I get compile-time name verification.

I'm pondering the idea of creating a Binding subclass that taps into some kind of scoped focus management container upon creation.  You could then use that Binding extension to have the binding automatically register itself with the entity responsible for satisfying focus change requests from VM.

Tally ho!
Josh

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

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:

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:

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:

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:

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

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

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:

Same here.

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

I prefer option 1 actually

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

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

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

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

On Mon, Mar 15, 2010 at 8:25 PM, Daniel Vaughan <dbvaug...@gmail.com> wrote:

Good thinking Marlon. As Pete said, very innovative.

On Mar 15, 7:33 pm, Marlon Grech ...

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.
Josh Smith  
View profile  
 More options Mar 16 2010, 12:38 pm
From: Josh Smith <flappleja...@gmail.com>
Date: Tue, 16 Mar 2010 09:38:34 -0700
Local: Tues, Mar 16 2010 12:38 pm
Subject: Re: [WPF Disciples] Re: Controlling focus from ViewModel objects

No problem, Philipp.

...

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

Hey Philipp...I changed some names in the code that I posted on my blog.
 IFocusController became IFocusMover, FocusControl became FocusController,
etc.  You might want to get the latest code, so that our blogs are in sync.

Josh

...

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.
Philipp Sumi  
View profile  
 More options Mar 16 2010, 2:38 pm
From: "Philipp Sumi" <phil...@hardcodet.net>
Date: Tue, 16 Mar 2010 19:38:43 +0100
Local: Tues, Mar 16 2010 2:38 pm
Subject: RE: [WPF Disciples] Re: Controlling focus from ViewModel objects

Josh,

I've already downloaded the sample from your blog and will make the post tomorrow (I like the cleanup and changed naming scheme, btw). I'll have to make the private class internal in order to get to it, but apart from that, it's ready to be used :)

Another thing - while looking at the source, I noticed that my decorator class was still the original version as it was published back in 2008. This one works, but it's missing the additional properties that were added with .NET 3.5 SP1 (e.g. StringFormat). I've updated the download in the mean time, so if you wanted to include the latest version, you could get it here: http://www.hardcodet.net/uploads/2008/04/custom-bindings.zip

Cheers,
Philipp

From: wpf-disciples@googlegroups.com [mailto:wpf-disciples@googlegroups.com] On Behalf Of Josh Smith
Sent: Dienstag, 16. März 2010 18:44
To: wpf-disciples@googlegroups.com
Subject: Re: [WPF Disciples] Re: Controlling focus from ViewModel objects

Hey Philipp...I changed some names in the code that I posted on my blog.  IFocusController became IFocusMover, FocusControl became FocusController, etc.  You might want to get the latest code, so that our blogs are in sync.

Josh

On Tue, Mar 16, 2010 at 9:38 AM, Josh Smith <flappleja...@gmail.com> wrote:

No problem, Philipp.

On Tue, Mar 16, 2010 at 9:15 AM, Philipp Sumi <phil...@hardcodet.net> wrote:

Josh,

Glad you could use it the helper class - cheers on the article :)
I was thinking about quickly publishing the alternative behavior approach, focusing on the behaviors as an alternative. Would that be okay for you if I just published it as an derivative of your legwork (including your VM and helper classes)?

Cheers,
Philipp

From: wpf-disciples@googlegroups.com [mailto:wpf-disciples@googlegroups.com] On Behalf Of Josh Smith
Sent: Dienstag, 16. März 2010 15:02

To: wpf-disciples@googlegroups.com
Subject: Re: [WPF Disciples] Re: Controlling focus from ViewModel objects

Philipp, that binding decorator class is exactly what I need.  Thanks!

On Tue, Mar 16, 2010 at 1:23 AM, Philipp Sumi <phil...@hardcodet.net> wrote:

Very nice approach, Josh - markup extensions are one of the really underestimated tools in a WPF developer's toolbox. In case you want to save yourself the plumbing of all the binding properties, you can take the source of this decorator class here - I'm using that one quite often for similar scenarios, but encapsulates the whole binding stuff in a base class: http://www.hardcodet.net/2008/04/wpf-custom-binding-class

However, maybe one could solve the problem more designer (less XAML) friendly using Blend behaviors? I gonna have to check that out.

From: wpf-disciples@googlegroups.com [mailto:wpf-disciples@googlegroups.com] On Behalf Of Josh Smith
Sent: Dienstag, 16. März 2010 04:41
To: wpf-disciples@googlegroups.com
Subject: Re: [WPF Disciples] Re: Controlling focus from ViewModel objects

On second thought, ProvideValue should check if the Binding property is null, and act like a {Binding} if it is:

public override object ProvideValue(IServiceProvider serviceProvider)
{
    var provideValueTarget = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
    if (provideValueTarget != null)
    {
        var element = provideValueTarget.TargetObject as DependencyObject;
        var property = provideValueTarget.TargetProperty as DependencyProperty;
        FocusControl.SetFocusableProperty(element, property);
    }

    if (this.Binding == null)
        this.Binding = new Binding();

    return this.Binding.ProvideValue(serviceProvider);

}
On Mon, Mar 15, 2010 at 7:38 PM, Josh Smith <flappleja...@gmail.com> wrote:

I took a stab at the custom binding approach that I mentioned earlier.  The new code is attached.  Here's all that's required in the XAML to make an element be a target for focus movement.

<TextBox
    Text="{local:FocusBinding Binding={Binding Path=FirstName, ValidatesOnDataErrors=True}}"
    />

Technically, the ValidatesOnDataErrors isn't needed.  

Here's how FocusBinding works:

public class FocusBinding : MarkupExtension
{
    public FocusBinding()
    {
    }

    public Binding Binding { get; set; }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        var provideValueTarget = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
        if (provideValueTarget != null)
        {
            var element = provideValueTarget.TargetObject as DependencyObject;
            var property = provideValueTarget.TargetProperty as DependencyProperty;
            FocusControl.SetFocusableProperty(element, property);
        }
        return this.Binding.ProvideValue(serviceProvider);
    }

}

If BindingBase.ProvideValue wasn't sealed, I could have just overridden that method instead of creating a wrapper markup extension.  Oh well.

Josh

On Mon, Mar 15, 2010 at 6:33 PM, Josh Smith <flappleja...@gmail.com> wrote:

I finally got some time to look over your code samples, Marlon.  It's definitely a cool idea, and shows some creative thinking.  

With that said, I wouldn't use it.  I try to avoid naming elements as much as possible, and this technique relies on assigning names to elements.  Not only that, but the name of an element must match the name of the VM property to which it's "important" property is bound.  I can see this leading to refactoring problems, learnability degradation, and general maintainability issues.  It's too magical, you f'ing Wizard you! ;-)

The reason I used the approach shown in my original code spike is threefold.

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

Fold #1 - By specifying the dependency property to which the validated VM property is bound, I avoid unnecessary duplication of VM property names in the View.

Fold #2 - There is no need to walk down the element tree, searching for elements that match a criterion.  This improves performance, especially in obese UIs.

Fold #3 - By making the 'ValidatedProperty' attached DP of type DependencyProperty, I get compile-time name verification.

I'm pondering the idea of creating a Binding subclass that taps into some kind of scoped focus management container upon creation.  You could then use that Binding extension to have the binding automatically register itself with the entity responsible for satisfying focus change requests from VM.

Tally ho!
Josh

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

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:

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:

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:

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:

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

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

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:

Same here.

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

I prefer option 1 actually

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

P.S sorry for ugly code and all that but the
...

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.
Josh Smith  
View profile  
 More options Mar 16 2010, 2:46 pm
From: Josh Smith <flappleja...@gmail.com>
Date: Tue, 16 Mar 2010 11:46:22 -0700
Local: Tues, Mar 16 2010 2:46 pm
Subject: Re: [WPF Disciples] Re: Controlling focus from ViewModel objects

Good to know, thanks!

On Tue, Mar 16, 2010 at 11:38 AM, Philipp Sumi <phil...@hardcodet.net>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.
Messages 26 - 50 of 71 < Older  Newer >
« Back to Discussions « Newer topic     Older topic »