我有一个Silverlight2应用程序验证数据OnTabSelectionChanged.我立刻开始的希望,UpdateSourceTrigger允许更多的不仅仅是LostFocus因为如果你击的标签,而不跳格的控制皇宫的对象是没有更新以前验证。

我的工作围绕这一问题对于文本框通过设置关注的另一个控制然后回OnTextChanged:

Private Sub OnTextChanged(ByVal sender As Object, ByVal e As TextChangedEventArgs)
    txtSetFocus.Focus()
    sender.Focus()
End Sub

现在我想完成同样的黑客在数据表格.我使用网格DataTemplates在运行时产生的CellTemplate和CellEditingTemplate.我试着写的TextChanged="OnTextChanged"入文本的数据模板,但它不是触发的。

任何人有任何想法?

有帮助吗?

解决方案

你可以做到这一行为的适用文本框太

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

其他提示

此博客文章介绍了如何使用附加属性显式更新文本框的来源: http ://www.thomasclaudiushuber.com/blog/2009/07/17/here-it-is-the-updatesourcetrigger-for-propertychanged-in-silverlight/

您可以轻松修改它以与其他控件一起使用...

我使用MVVM和Silverlight 4遇到了同样的问题。问题是绑定在文本框失去焦点之前不会更新源,但是将焦点设置在另一个控件上并不起作用。

我找到了一个使用两个不同博客文章组合的解决方案。我使用了Patrick Cauldwell的DefaultButtonHub概念中的代码,其中一个是“SmallWorkaround”。来自SmallWorkarounds.net

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

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

我的更改导致DefaultButtonHub类的以下代码:

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

}

这应包含在Silverlight的某些文档中:)

我知道这是个老消息......但我这样做是为了解决这个问题:

Text =&quot; {Binding Path = newQuantity,UpdateSourceTrigger = PropertyChanged}&quot;

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top