我需要访问组合框的非公开成员(突出显示的项目) [英] I need to access a non-public member (Highlighted Item) of a Combo Box
本文介绍了我需要访问组合框的非公开成员(突出显示的项目)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在为应用程序实现按键导航,我想在组合框聚焦时覆盖空格键功能,使其像回车键一样;像这样:
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屋!
查看全文