Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 以编程方式添加Interaction.Behavior_C#_Silverlight_Windows Phone 7_Windows Phone 8 - Fatal编程技术网

C# 以编程方式添加Interaction.Behavior

C# 以编程方式添加Interaction.Behavior,c#,silverlight,windows-phone-7,windows-phone-8,C#,Silverlight,Windows Phone 7,Windows Phone 8,我正在尝试为Windows Phone 8(Silverlight)创建一个文本框,其行为类似于具有特殊行为的普通文本框。 其行为是,只要用户键入,ViewModel就会更新,而不是在文本框失去焦点时更新。 这是我现在拥有的并且正在工作的东西 <TextBox Text="{Binding Path=Email,Mode=TwoWay}" InputScope="EmailNameOrAddress"> <i:Interaction.Behaviors>

我正在尝试为Windows Phone 8(Silverlight)创建一个文本框,其行为类似于具有特殊行为的普通文本框。
其行为是,只要用户键入,ViewModel就会更新,而不是在文本框失去焦点时更新。
这是我现在拥有的并且正在工作的东西

<TextBox Text="{Binding Path=Email,Mode=TwoWay}" InputScope="EmailNameOrAddress">
    <i:Interaction.Behaviors>
        <helpers:UpdateTextBindingOnPropertyChanged />
    </i:Interaction.Behaviors>
</TextBox> 
但是我的行为有一个错误。
我使用的行为使用以下代码确定其表达式(失败)


我该怎么做

问题是:只要您没有为
FastTextbox.Text
属性设置任何绑定,
GetBindingExpression
将返回null。这是绝对正确的行为。还没有绑定表达式

[编辑] 一种解决方案可能是:可能存在对OnTextChanged或OnTextPropertyChanged的覆盖,您可以在那里调用
GetBindingExpression
方法

[编辑二]
您可以使用
Text=“{Binding Path=Email,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}”
?对快速文本框的需求将停止。

实际解决方案非常简单:)


在我看来,还是您在TextBox构造函数中缺少InitializeComponent?@verdesrobert:(免责声明:我不为WP7/8编程,但假设他们的Silverlight框架提供了类似于桌面版本的API)
InitializeComponent
仅用于用户控件。
public class FastTextbox:System.Windows.Controls.TextBox
{
    public FastTextbox()
    {
        BehaviorCollection Behaviors= Interaction.GetBehaviors(this);
        Behaviors.Add(new UpdateTextBindingOnPropertyChanged());
    }
}
protected override void OnAttached()
{
    base.OnAttached();

    // expression gets null here :(
    _expression = AssociatedObject.GetBindingExpression(TextBox.TextProperty);
    AssociatedObject.TextChanged += OnTextChanged;
}
    public FastTextbox()
    {
        TextChanged += OnTextBoxTextChanged;
    }

    private void OnTextBoxTextChanged(object sender, TextChangedEventArgs e)
    {
        TextBox senderText = (TextBox)sender;
        BindingExpression bindingExp = senderText.GetBindingExpression(TextProperty);
        bindingExp.UpdateSource();
    }