Answered Binding.UpdateSourceTrigger

  • Thursday, March 13, 2008 3:50 PM
     
     

    Hi 

    what about Binding.UpdateSourceTrigger? It seems like controls can only bind when they lose their focus. WPF allows also binding in realtime (UpdateSourceTrigger=PropertyChange). Is this possible with Silverlight?

    Regards
    Marco Buerckel

All Replies

  • Thursday, March 13, 2008 4:00 PM
     
     Answered

    Nevermind, just built an extension that does the job. Anyway, it would be great to see that feature within the next beta...

    Regards
    Marco Buerckel

  • Monday, June 30, 2008 12:21 PM
     
     

    How did you achieve this?

     

    Thanks,

    Andy

     

  • Tuesday, March 03, 2009 10:41 PM
     
     
    Hi,

    I'd be very interested in knowing how you did this. Would you mind posting the code to your extension, or telling us how you faked the UpdateSourceTrigger?

    Thanks,

    Romain
  • Tuesday, March 03, 2009 11:31 PM
     
     

    Hi Romain and Andy,

    I just created one small sample for you guys. I make it works only for Textbox. You guys need to customize it based on your requirement.

    UpdateSourceTriggerHelper - Attached Properties

     public class UpdateSourceTriggerHelper
        {
            public static readonly DependencyProperty UpdateSourceTriggerProperty =
                DependencyProperty.RegisterAttached("UpdateSourceTrigger", typeof(bool), typeof(UpdateSourceTriggerHelper),
                                                    new PropertyMetadata(OnUpdateSourceTriggerChanged));

            public static bool GetUpdateSourceTrigger(DependencyObject d)
            {
                return (bool)d.GetValue(UpdateSourceTriggerProperty);
            }


            public static void SetUpdateSourceTrigger(DependencyObject d, bool value)
            {
                d.SetValue(UpdateSourceTriggerProperty, value);
            }

            private static void OnUpdateSourceTriggerChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                TextBox textBox = d as TextBox;
                if ((bool)e.OldValue)
                {
                    textBox.TextChanged -= (s, arg) => {
                           
                     };
                }
                if ((bool)e.NewValue)
                {
                    textBox.TextChanged += (s, arg) => {
                        var c = findFocusableControl(textBox);
                        if (c != null)
                        {
                            c.Focus();
                        }
                        textBox.Focus();
                     };
                }
            }

            private static Control findFocusableControl(Control control)
            {
               var ctl = VisualTreeHelper.GetParent(control);
               if ((ctl as Control) != null)
               {
                   return ctl as Control;
               }
               else
               {
                   int childrenCount = VisualTreeHelper.GetChildrenCount(ctl);
                   for (int i = 0; i < childrenCount; i++)
                   {
                       var c = VisualTreeHelper.GetChild(ctl, i) as Control;
                       if ((c != null) && (c != control))
                       {
                           return c;
                       }
                   }
               }
               return null;
            }
        }

     

    Usage :

    <UserControl x:Class="UpdateSourceTriggerExtDemo.Page"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:UpdateSourceTriggerExtDemo"        
        Width="400" Height="300">
        <Grid x:Name="LayoutRoot" Background="White">
            <StackPanel>
                <TextBox x:Name="nameTextbox" Height="25" Width="100" Margin="5" Text="{Binding Name, Mode=TwoWay}"
                        local:UpdateSourceTriggerHelper.UpdateSourceTrigger="True" />
                <TextBox x:Name="addressTextbox" Height="25" Width="100" Margin="5" Text="{Binding Address, Mode=TwoWay}"
                        local:UpdateSourceTriggerHelper.UpdateSourceTrigger="False" />
                <TextBox x:Name="phoneTextbox" Height="25" Width="100" Margin="5" Text="{Binding Phone, Mode=TwoWay}"
                         />
                <Button Height="25" Width="100" Margin="5" Content="Save" />
            </StackPanel>
           
           
        </Grid>
    </UserControl>

  • Thursday, March 19, 2009 12:31 AM
     
     

    Thanks mchlsync!

    This is a great way of automating the focus trick. I just have to do some research on how to set attached properties programmatically (my TextBox is created in the code), but it looks very promising!

     

  • Friday, July 17, 2009 8:11 AM
     
     

    There's also another solution than using the focus trick. Try it via updating the BindingSource directly:

     http://www.thomasclaudiushuber.com/blog/2009/07/17/here-it-is-the-updatesourcetrigger-for-propertychanged-in-silverlight/

     

  • Thursday, July 23, 2009 5:12 AM
     
     

     I have created a workaround using behaviors. Here is the blogpost, and you can download the code as well:

    Binding update on TextBox.TextChanged event using Behaviors

  • Monday, December 14, 2009 10:48 AM
     
     
  • Wednesday, June 02, 2010 3:34 AM
     
     

    Were you able to set attached properties programmatically? Can you please share with me the sample?

  • Wednesday, June 02, 2010 7:13 AM
     
     

    Of course you can set them programmatically. Use the SetValue-Method inherited from DependencyObject.

    E.g.

    myTextBox.SetValue(BindingHelper.UpdateSourceOnChangeProperty,true);