Skip to main content

Microsoft Silverlight

Answered Question Binding.UpdateSourceTriggerRSS Feed

(0)

Marco Buerckel
Marco Bu...

Member

Member

12 points

4 Posts

Binding.UpdateSourceTrigger

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

Don't forget to mark posts that were helpful as answers.

Marco Buerckel
Marco Bu...

Member

Member

12 points

4 Posts

Answered Question

Re: Binding.UpdateSourceTrigger

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

Don't forget to mark posts that were helpful as answers.

andyboyne
andyboyne

Member

Member

105 points

56 Posts

Re: Binding.UpdateSourceTrigger

How did you achieve this?

 

Thanks,

Andy

 

RomainP
RomainP

Member

Member

39 points

44 Posts

Re: Binding.UpdateSourceTrigger

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

mchlsync
mchlsync

Star

Star

14566 points

2,730 Posts

Silverlight MVP

Re: Binding.UpdateSourceTrigger

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>

(If this has answered your question, please click on "Mark as Answer" on this post. Thank you!)

Regards,
Michael Sync
Silverlight MVP

Blog : http://michaelsync.net


RomainP
RomainP

Member

Member

39 points

44 Posts

Re: Binding.UpdateSourceTrigger

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!

 

Thomas Claudius Huber
Thomas C...

Member

Member

76 points

20 Posts

Re: Binding.UpdateSourceTrigger

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/

 

Thomas Claudius Huber
http://www.thomasclaudiushuber.com
also try http://www.thomasclaudiushuber.com/pageInSilverlight ;-)

Zoltan Arvai
Zoltan A...

Member

Member

2 points

1 Posts

Re: Binding.UpdateSourceTrigger

 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

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities