Windows Phone 7 ListBox 事件混淆

发布于 2024-12-12 13:28:39 字数 265 浏览 0 评论 0原文

该应用程序就像一本小词典。我有一个列表框和一个文本框。列表框已填充有单词,当文本框中有任何条目时,列表框将再次填充以文本框中的字母开头的单词。我有一个列表框 SelectionChanged 事件,当用户单击一个单词时,它的含义就会出现。问题是,当用户从列表中选择一个单词,然后在文本框中键入某些内容时,将调用 listBox SelectionChanged 事件,我不希望发生这种情况,因为此时我的列表框的所选项目为空。我想要一个事件仅当用户从列表框中选择某些内容时才会触发。当列表框的内容更改时不应触发它。谢谢

The app is like a small dictionary. I have a listbox and a textbox. The list box is already filled with words and when there is any entry in the textbox the listbox is refilled again with words starting with the letters in the textbox. I have a listbox SelectionChanged event implemented when the user clicks on a word its meaning appears. The problem is when user selects a word from the list and then types something in the textbox, listBox SelectionChanged event is called i dont want this to happen because at this point of time my listbox's selected item is empty.I would like to have a event that is fired only when user selects something from the listbox. It should not be fired when the content of the listbox changes. Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感性不性感 2024-12-19 13:28:39

您可以使用

1.if (lstWords.SelectedItem != null)

2.lstWords.SelectedIndex = -1;

例如< /strong> 以下是文本更改事件和列表选择更改事件的源代码

     private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
    {
        try
        {
            if (textBox1.Text.ToString().Equals(""))
            {
                XmlDictionaryRepository test = new XmlDictionaryRepository();
                lstWords.ItemsSource = test.GetWordList(categorySelected,xmlFileName);
            }
            else
            {
                XmlDictionaryRepository test = new XmlDictionaryRepository();
                lstWords.ItemsSource = test.GetMatchWordList(categorySelected, textBox1.Text.ToString(),xmlFileName);
            }

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message.ToString(), (((PhoneApplicationFrame)Application.Current.RootVisual).Content).ToString(), MessageBoxButton.OK);
        }
    }

    private void lstWords_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

        try
        {
            if (lstWords.SelectedItem != null)
            {             

                string wordSelected = ((Glossy_Test.Dictionary)(lstWords.SelectedItem)).Word;
                if (lstWords.SelectedItem != null)
                {
                    NavigationService.Navigate(new Uri(string.Format("/DescribeWord.xaml?param1={0}¶m2={1}", wordSelected, categorySelected), UriKind.Relative));
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message.ToString(), (((PhoneApplicationFrame)Application.Current.RootVisual).Content).ToString(), MessageBoxButton.OK);
        }
        finally
        {
            // lstWords.SelectedIndex = -1;
        }

    }

You can use

1.if (lstWords.SelectedItem != null)

2.lstWords.SelectedIndex = -1;

for e.g. following is the source code for text changed event and list selection change event

     private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
    {
        try
        {
            if (textBox1.Text.ToString().Equals(""))
            {
                XmlDictionaryRepository test = new XmlDictionaryRepository();
                lstWords.ItemsSource = test.GetWordList(categorySelected,xmlFileName);
            }
            else
            {
                XmlDictionaryRepository test = new XmlDictionaryRepository();
                lstWords.ItemsSource = test.GetMatchWordList(categorySelected, textBox1.Text.ToString(),xmlFileName);
            }

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message.ToString(), (((PhoneApplicationFrame)Application.Current.RootVisual).Content).ToString(), MessageBoxButton.OK);
        }
    }

    private void lstWords_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

        try
        {
            if (lstWords.SelectedItem != null)
            {             

                string wordSelected = ((Glossy_Test.Dictionary)(lstWords.SelectedItem)).Word;
                if (lstWords.SelectedItem != null)
                {
                    NavigationService.Navigate(new Uri(string.Format("/DescribeWord.xaml?param1={0}¶m2={1}", wordSelected, categorySelected), UriKind.Relative));
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message.ToString(), (((PhoneApplicationFrame)Application.Current.RootVisual).Content).ToString(), MessageBoxButton.OK);
        }
        finally
        {
            // lstWords.SelectedIndex = -1;
        }

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文