In a combobox, how do I determine the highlighted item (not selected item)?

10 篇文章 0 订阅

方法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:

alt text


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值