Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I ran into this same problem using MVVM and Silverlight 4. The problem is that the binding does not update the source until after the textbox looses focus, but setting focus on another control doesn't do the trick.</p> <p>I found a solution using a combination of two different blog posts. I used the code from Patrick Cauldwell's DefaultButtonHub concept, with one "SmallWorkaround" from SmallWorkarounds.net</p> <p><a href="http://www.cauldwell.net/patrick/blog/DefaultButtonSemanticsInSilverlightRevisited.aspx" rel="nofollow noreferrer">http://www.cauldwell.net/patrick/blog/DefaultButtonSemanticsInSilverlightRevisited.aspx</a></p> <p>www.smallworkarounds.net/2010/02/elementbindingbinding-modes.html</p> <p>My change resulted in the following code for the DefaultButtonHub class:</p> <pre><code>public class DefaultButtonHub { ButtonAutomationPeer peer = null; private void Attach(DependencyObject source) { if (source is Button) { peer = new ButtonAutomationPeer(source as Button); } else if (source is TextBox) { TextBox tb = source as TextBox; tb.KeyUp += OnKeyUp; } else if (source is PasswordBox) { PasswordBox pb = source as PasswordBox; pb.KeyUp += OnKeyUp; } } private void OnKeyUp(object sender, KeyEventArgs arg) { if (arg.Key == Key.Enter) if (peer != null) { if (sender is TextBox) { TextBox t = (TextBox)sender; BindingExpression expression = t.GetBindingExpression(TextBox.TextProperty); expression.UpdateSource(); } ((IInvokeProvider)peer).Invoke(); } } public static DefaultButtonHub GetDefaultHub(DependencyObject obj) { return (DefaultButtonHub)obj.GetValue(DefaultHubProperty); } public static void SetDefaultHub(DependencyObject obj, DefaultButtonHub value) { obj.SetValue(DefaultHubProperty, value); } // Using a DependencyProperty as the backing store for DefaultHub. This enables animation, styling, binding, etc... public static readonly DependencyProperty DefaultHubProperty = DependencyProperty.RegisterAttached("DefaultHub", typeof(DefaultButtonHub), typeof(DefaultButtonHub), new PropertyMetadata(OnHubAttach)); private static void OnHubAttach(DependencyObject source, DependencyPropertyChangedEventArgs prop) { DefaultButtonHub hub = prop.NewValue as DefaultButtonHub; hub.Attach(source); } } </code></pre> <p>This should be included in some sort of documentation for Silverlight :)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload