我需要访问一个组合框的非公共成员(突出显示的项目) [英] I need to access a non-public member (Highlighted Item) of a Combo Box

查看:343
本文介绍了我需要访问一个组合框的非公共成员(突出显示的项目)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个应用程序实现键导航,当一个组合框被聚焦时,我想覆盖空格键的功能,使它像一个回车键;像这样:

I am implementing Key Navigation for an application and I want to override the space key functionality when a Combo Box is focused such that it acts like an enter key; like this:

if (!cb.IsDropDownOpen)
{
  cb.IsDropDownOpen = true;
}
else
{
  cb.SelectedItem = cb.{non-public member HighlightedItem};
  cb.IsDropDownOpen = false;
}

问题是我需要获得非公共成员的值所以我可以设置所选的值并关闭下拉(输入通常如何工作)。

The problem is that I need to get the value of that non-public member so that I can set the selected value and close the drop-down (how enter would normally work).

现在的问题是:什么是最快,最麻烦的自由实现方式?

推荐答案

您必须使用反射来获取属性的值

You'd have to use reflection to get the value of the property

PropertyInfo highlightedItemProperty = cb.GetType().GetProperties(BindingFlags.NonPublic  | BindingFlags.Instance).Single(pi => pi.Name == "HighlightedItem");
object highlightedItemValue = highlightedItemProperty.GetValue(cb, null);

要浏览所有属性或字段,

To browse all properties or fields, also check out

var allProps = cb.GetType().GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).OrderBy(pi => pi.Name).ToList();
var allFields = cb.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).OrderBy(pi => pi.Name).ToList();

(您可以在调试器中阅读所有内容)

(you can read through them all in the debugger)

这篇关于我需要访问一个组合框的非公共成员(突出显示的项目)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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