来自wpf combobox SelectionChanged事件的选定文本? [英] Selected Text from wpf combobox SelectionChanged event?

查看:646
本文介绍了来自wpf combobox SelectionChanged事件的选定文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在用户控件(wpf)中有一个ComboBox,我已经连接到vb.net中的SelectionChangedEventHandler,以便在组合框更改时触发Sub。这一切都很好,但我找到了一些东西。显然,无论是什么触发ComboBox更新它的.Text值都不会发生,直到我抓住的SlectionChangedEvent(来自经验和Google),所以当我有那个文本值报告时,我得到的是前一个值而不是新选择的值。 br />


我有几个关于如何解决这个问题的线索,但我不知道该怎么办。



想法1:

也许我可以获取.SelectedIndex值并导出与索引关联的文本,如果可能的话,我不知道如何使这个工作。



创意2:

我不明白这是如何或为什么有效,甚至不知道在哪里实施。但有人在这里 http://leeontech.wordpress。 com / 2008/02/26 / the-the-selecteditem-in-combobox / 正在讨论使用DependencyPropertyDescriptor作为解决方案。



也许这些都不是正确的方法,我不知道,所以我感谢任何帮助。



谢谢,

Jason

解决方案

你应该明白,只要你点击Enter(或双击),当用户排序时,选择不会改变。 确认了选择。只触发此事件。当您只是在没有确认的情况下移动您的选择时,不会发生此类事件。



如果你需要捕获这个事件,你应该创建它 - 它不存在于 ComboBox 中。 CodeProject中有一个解决方案。我们处理所有键盘和鼠标事件,并在每个事件上计算选择的位置(与 SelectedIndex 不同,因为选择还没有这是相当复杂和消耗的代码。你确定你需要吗?根据我的解释,请确定你想要处理的事件。



你的想法#1是正确的。这是获取列表元素的有效方法。文本不完全相同。考虑到:用户可以开始在控件的文本框中键入内容;此时文本可能不会匹配任何列表项。对于#2,它只是捕获文本的更改。如果你需要捕获索引更改并获取项目文本,这是不同的事件。这一切都取决于你想得到什么(见上面)。



-SA


最后我在这里解决了我的问题。我不知道我在做这件事的方式是否有任何陷阱,但它看到了ms工作。组合框仅限于预定义的下拉项目。

 私有  Sub  Window_Loaded( ByVal  sender  As  System。对象 ByVal  e  As  System.Windows.RoutedEventArgs)句柄  MyBase  .Loaded 
ComboBox1.Items.Add( 一个
ComboBox1.Items.Add( two
ComboBox1.Items.Add( three
结束 Sub

私有 Sub ComboBox1_SelectionChanged( ByVal sender As System。对象 ByVal e As System.Windows.Controls.SelectionChangedEventArgs)句柄 ComboBox1.SelectionChanged
Dim indexnum As < span class =code-keyword> Integer = ComboBox1.SelectedIndex
MsgBox(indexnum& & ComboBox1.Items(indexnum))
结束 Sub
结束



这是测试代码,但它证明了我的概念想。


I have a ComboBox in a user control(wpf) where I have hooked on to the SelectionChangedEventHandler in vb.net so that a Sub is triggered whenever the combo box changes. This all works good and well, however I found something out. Apparently whatever triggers the ComboBox to update it's .Text value does not happen until after the the SlectionChangedEvent I am grabbing (from experience and Google) so when I have that text value report back I get the previous value instead of the newly selected value.

I have a couple leads as to how to solve this but I'm not sure how to do either.

Idea 1:
Perhaps I can take the .SelectedIndex value and derive the text associated to the index, I have no idea how to make this work if it is possible.

Idea 2:
I didn't understand how or why this worked, or even where to implement it. But somebody here http://leeontech.wordpress.com/2008/02/26/getting-the-text-of-the-selecteditem-in-combobox/ is talking about using DependencyPropertyDescriptor as a solution.

Maybe neither of these is the right way to go about it, I don't know, so I appreciate any help .

Thank you,
Jason

解决方案

You should understand that selection is not changed until you hit Enter (or double click), when the user sort of "confirms" the selection. Only this event is fired. There is no such event which would fire when you simply move your selection without confirmation.

If you need to capture this event, you should create it — it does not exist in ComboBox. There was a solution somewhere in the CodeProject. We process all keyboard and mouse events and on each event calculate where the selection is (which is not the same as SelectedIndex as the selection is not yet confirmed. This is pretty complex and consuming code. Are you sure you need it? Based on my explanations, please decide what exactly event you want to handle.

You Idea #1 is correct. This is the robust way to get the element of list. Text is not exactly the same thing. Take into account: the user can start typing in the text box part of the control; at this moment the text may not match any of the list items. As to #2, it simply captures the change of text. If you need to capture index change and get the items text, this is the different event. It all depends on what you want to get (see above).

—SA


Ultimately here is what I did to solve my problem. I don't know if there are any pitfalls in the way I am doing it but it seems to be working. The combobox is restricted to only predefined pulldown items.

Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
        ComboBox1.Items.Add("one")
        ComboBox1.Items.Add("two")
        ComboBox1.Items.Add("three")
    End Sub

Private Sub ComboBox1_SelectionChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles ComboBox1.SelectionChanged
        Dim indexnum As Integer = ComboBox1.SelectedIndex
        MsgBox(indexnum & " " & ComboBox1.Items(indexnum))
    End Sub
End Class


This is test code but it proves the concept I think.


这篇关于来自wpf combobox SelectionChanged事件的选定文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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