Silverlight 5 + AutoCompleteBox = 错误 [英] Silverlight 5 + AutoCompleteBox = Bug

查看:24
本文介绍了Silverlight 5 + AutoCompleteBox = 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚刚安装了几天前发布的SL5和工具包.
当您将 AutoCompleteBox 的 Text 属性设置为 string.Empty 时,就会发生该错误.它会导致 AutoCompleteBox 处于错误状态.重现错误:

Just installed SL5 and the toolkit, that were released few days ago.
The bug happens when you set the Text property of the AutoCompleteBox to string.Empty. It causes the AutoCompleteBox to be in a buggy state. To reproduce the bug:

在主页上添加一个 AutoCompleteBox 和一个按钮.注册到 TextChanged 和 Click 事件.这是代码隐藏:

add an AutoCompleteBox and a Button to the main page. Register to the TextChanged and Click events. This is the code-behind:

public partial class MainPage : UserControl
{
    public MainPage()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        auto.Text = string.Empty;
    }

    private void auto_TextChanged(object sender, RoutedEventArgs e)
    {
        // Put a break point here.
    }
} 

在运行时:

1) 在自动框中输入aa".

1) type "aa" into the autobox.

2) 点击按钮.

3) 输入q".(仍然调用 TextChanged).

3) type "q". ( TextChanged is still invoked).

4) 擦除q" - TextChanged 被调用.

4) erase the "q" - TextChanged is not invoked.

5) 再次输入q" - TextChanged 被调用.

5) type "q" again - TextChanged is not invoked.

6) 依此类推,直到您选择一个新字母.然后一切重新开始.

6) and so on, until you pick a new letter. And then it's starts over.

推荐答案

我找到了解决这种奇怪行为的方法.您需要一个从 AutoCompleteBox 派生的控件并覆盖 OnApplyTemplate 方法来查找 AutoCompleteBox 的内部 TextBox.

I found a workaround for this strange behavior. You need a control derived from AutoCompleteBox and overrride OnApplyTemplate method to find inner TextBox of AutoCompleteBox.

当内部 TextBox TextChanged 事件触发时,您需要手动触发 AutoCompleteBox 控件的 TextChanged 事件.

When inner TextBox TextChanged event fires you need to fire TextChanged event of AutoCompleteBox control manually.

public class CustomAutoComplete : AutoCompleteBox
{
    TextBox mytext;

    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
        mytext = GetTemplateChild("Text") as TextBox;
        mytext.TextChanged += new System.Windows.Controls.TextChangedEventHandler(mytext_TextChanged);
    }

    void mytext_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
    {
        this.Text = mytext.Text;
        OnTextChanged(new RoutedEventArgs());
    }
}

这篇关于Silverlight 5 + AutoCompleteBox = 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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