Silverlight Datagrid上UpdateSourceTrigger LostFocus的解决方法? [英] Workaround for UpdateSourceTrigger LostFocus on Silverlight Datagrid?

查看:176
本文介绍了Silverlight Datagrid上UpdateSourceTrigger LostFocus的解决方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Silverlight 2应用程序验证数据OnTabSelectionChanged。立即我开始希望UpdateSourceTrigger允许的不仅仅是LostFocus,因为如果您在不勾选控件的情况下单击该选项卡,则在验证之前不更新LINQ对象。



我通过将焦点设置为另一个控件,然后重新启动OnTextChanged:

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

现在我试图在DataGrid中完成同样的攻击。我的DataGrid使用在运行时生成的CellTemplate和CellEditingTemplate的DataTemplates。我尝试将TextChanged =OnTextChanged写入DataTemplate中的TextBox,但不会触发。



任何人都有任何想法?

解决方案

你可以通过应用于文本框的行为来做到这一点

  // xmlns:int是System.Windows.Interactivity.DLL中的System.Windows.Interactivity 
// xmlns:behavior是您下面的类的命名空间
< 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();
}
}


I have a Silverlight 2 application that validates data OnTabSelectionChanged. Immediately I began wishing that UpdateSourceTrigger allowed more than just LostFocus because if you click the tab without tabbing off of a control the LINQ object is not updated before validation.

I worked around the issue for TextBoxes by setting focus to another control and then back OnTextChanged:

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

Now I am trying to accomplish the same sort of hack within a DataGrid. My DataGrid uses DataTemplates generated at runtime for the CellTemplate and CellEditingTemplate. I tried writing the TextChanged="OnTextChanged" into the TextBox in the DataTemplate, but it is not triggered.

Anyone have any ideas?

解决方案

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

这篇关于Silverlight Datagrid上UpdateSourceTrigger LostFocus的解决方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆