Workaround for UpdateSourceTrigger LostFocus on Silverlight Datagrid?

社会主义新天地 提交于 2019-12-05 11:01:13

You can do it with a behavior applied to the textbox too

// xmlns:int is System.Windows.Interactivity from System.Windows.Interactivity.DLL)
// xmlns:behavior is your namespace for the class below
<TextBox Text="{Binding Description,Mode=TwoWay,UpdateSourceTrigger=Explicit}">
    <int:Interaction.Behaviors>
       <behavior:TextBoxUpdatesTextBindingOnPropertyChanged />
    </int:Interaction.Behaviors>
</TextBox>


public class TextBoxUpdatesTextBindingOnPropertyChanged : Behavior<TextBox>
{
    protected override void OnAttached()
    {
        base.OnAttached();

        AssociatedObject.TextChanged += new TextChangedEventHandler(TextBox_TextChanged);
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();

        AssociatedObject.TextChanged -= TextBox_TextChanged;
    }

    void TextBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        var bindingExpression = AssociatedObject.GetBindingExpression(TextBox.TextProperty);
        bindingExpression.UpdateSource();
    }
}

This blog post shows how to update the source of a textbox explicitly using attached property: http://www.thomasclaudiushuber.com/blog/2009/07/17/here-it-is-the-updatesourcetrigger-for-propertychanged-in-silverlight/

You could easily modify it to work with other controls as well...

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.

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

http://www.cauldwell.net/patrick/blog/DefaultButtonSemanticsInSilverlightRevisited.aspx

www.smallworkarounds.net/2010/02/elementbindingbinding-modes.html

My change resulted in the following code for the DefaultButtonHub class:

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);
    }

}

This should be included in some sort of documentation for Silverlight :)

I know it's old news... but I got around this by doing this:

Text="{Binding Path=newQuantity, UpdateSourceTrigger=PropertyChanged}"

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!