Skip to main content
Home Forums Silverlight Programming Programming with .NET - General Binding.UpdateSourceTrigger
7 replies. Latest Post by Zoltan Arvai on July 23, 2009.
(0)
Marco Bu...
Member
12 points
4 Posts
03-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?
RegardsMarco Buerckel
03-13-2008 4:00 PM |
Nevermind, just built an extension that does the job. Anyway, it would be great to see that feature within the next beta...
andyboyne
105 points
56 Posts
06-30-2008 12:21 PM |
How did you achieve this?
Thanks,
Andy
RomainP
39 points
44 Posts
03-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
mchlsync
Star
14566 points
2,730 Posts
03-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>
03-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!
Thomas C...
76 points
20 Posts
07-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/
Zoltan A...
2 points
1 Posts
07-23-2009 5:12 AM |
I have created a workaround using behaviors. Here is the blogpost, and you can download the code as well: