Silverlight 5 + AutoCompleteBox = Bug

此生再无相见时 提交于 2019-12-17 20:51:51

问题


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:

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.
    }
} 

In runtime:

1) type "aa" into the autobox.

2) click the button.

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

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

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

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


回答1:


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.

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


来源:https://stackoverflow.com/questions/8488968/silverlight-5-autocompletebox-bug

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