In a combobox, how do I determine the highlighted item (not selected item)?
来源:互联网 发布:程序员挣钱的五种方法 编辑:程序博客网 时间:2024/05/16 09:51
方法1:
PropertyInfo highlightedItemProperty = cb.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Instance).Single(pi => pi.Name == "HighlightedItem");object highlightedItemValue = highlightedItemProperty.GetValue(cb, null);
方法2:const BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance; PropertyInfo hl = box.GetType().GetProperty("HighlightedItem", flags);
原帖:http://stackoverflow.com/questions/2723977/in-a-combobox-how-do-i-determine-the-highlighted-item-not-selected-item
First, fair warning: I am a complete newbie with C# and WPF.
I have a combobox (editable, searchable) and I would like to be able to intercept the Delete key and remove the currently highlighted item from the list. The behavior I'm looking for is like that of MS Outlook when entering in email addresses. When you give a few characters, a dropdown list of potential matches is displayed. If you move to one of these (with the arrow keys) and hit Delete, that entry is permanently removed. I want to do that with an entry in the combobox.
Here is the XAML (simplified):
<ComboBox x:Name="Directory" KeyUp="Directory_KeyUp" IsTextSearchEnabled="True" IsEditable="True" Text="{Binding Path=CurrentDirectory, Mode=TwoWay}" ItemsSource="{Binding Source={x:Static self:Properties.Settings.Default}, Path=DirectoryList, Mode=TwoWay}" />
The handler is:
private void Directory_KeyUp(object sender, KeyEventArgs e){ ComboBox box = sender as ComboBox; if (box.IsDropDownOpen && (e.Key == Key.Delete)) { TrimCombobox("DirectoryList", box.HighlightedItem); // won't compile! }}
When using the debugger, I can see box.HighlightedItem
has the value I want but when I try and put in that code, it fails to compile with:
System.Windows.Controls.ComboBox' does not contain a definition for 'HighlightedItem'...
So: how do I access that value? Keep in mind that the item has not been selected. It is merely highlighted as the mouse hovers over it.
Thanks for your help.
Here is a screenshot showing the debugger's display. I hovered over "box" and when the one-line summary was displayed, I then hovered over the + char to expand to this image:
The Highlighted Item property is a Non-Public member, so you can't call it from another class.
I believe you need to use Reflection to get at Non-Public members. Here's a SO post on the subject:http://stackoverflow.com/questions/769634/access-non-public-members-reflectionattribute
- In a combobox, how do I determine the highlighted item (not selected item)?
- How do I hide a menu item in the actionbar?
- How do I determine the expiration date of a p12 certificate?
- How do I bind the values of an enum to a ComboBox?
- How do I tell if a regular file does not exist in bash?
- Could not find a price list in Ordered UOM xxx and Primary UOM of the item
- FAQ How do I write to the console from a plug-in?
- How do I lock the orientation to portrait mode in a iPhone Web Application?
- How do I sort an array of hashes by a value in the hash?
- How to keep Item in the same page?
- How to display fields in the frontend item view
- How do I determine if a web page exists with shell scripting?
- 在keychain tool里,Request a certificate时,出错''The specified item could not be found in the keychain"
- How do I change the default runlevel in Feodra 15?
- How do I get the “Command Buffer” in Solaris 10?
- How do I register a custom filetype in iOS【链接】
- How do I run a 64-bit guest in VirtualBox?
- How do I instantiate a Queue object in java?
- hdu 2154 跳舞毯 (DP)
- GDAL的GDALDataset::GetGeoTransform()函数
- hdu 2151 Worm (DP)
- linux中运行脚本与设置环境变量
- hdu 2068 RPG的错排 (错排+组合)
- In a combobox, how do I determine the highlighted item (not selected item)?
- poj 2513 为什么 一直RE 啊!!!!!
- EL表达式在JSP中不能解析的问题
- hdu 2067 小兔的棋盘 (DP)
- hdu 2065 "红色病毒"问题 (母函数)
- centos 网络配置 小节
- hdu 1996 汉诺塔VI
- hdu 1995 汉诺塔V
- hdu 3308 LCIS 最大连续递增字串长 线段树区间合并