c# - 我需要访问组合框的非公共(public)成员(突出显示的项目)

标签 c# combobox wpf-controls overriding

我正在为一个应用程序实现按键导航,我想在组合框获得焦点时覆盖空格键功能,使其像回车键一样;像这样:

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

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

现在的问题是:实现此目标最快且最轻松的方法是什么?

最佳答案

这是我用于执行此操作的辅助类:

public static class PropertyHelper
{
    /// <summary>
    /// Returns a _private_ Property Value from a given Object. Uses Reflection.
    /// Throws a ArgumentOutOfRangeException if the Property is not found.
    /// </summary>
    /// <typeparam name="T">Type of the Property</typeparam>
    /// <param name="obj">Object from where the Property Value is returned</param>
    /// <param name="propName">Propertyname as string.</param>
    /// <returns>PropertyValue</returns>
    public static T GetPrivatePropertyValue<T>(this object obj, string propName)
    {
        if (obj == null) throw new ArgumentNullException("obj");
        PropertyInfo pi = obj.GetType().GetProperty(propName,
                                                    BindingFlags.Public | BindingFlags.NonPublic |
                                                    BindingFlags.Instance);
        if (pi == null)
            throw new ArgumentOutOfRangeException("propName",
                                                  string.Format("Property {0} was not found in Type {1}", propName,
                                                                obj.GetType().FullName));
        return (T) pi.GetValue(obj, null);
    }

    /// <summary>
    /// Returns a private Field Value from a given Object. Uses Reflection.
    /// Throws a ArgumentOutOfRangeException if the Property is not found.
    /// </summary>
    /// <typeparam name="T">Type of the Field</typeparam>
    /// <param name="obj">Object from where the Field Value is returned</param>
    /// <param name="propName">Field Name as string.</param>
    /// <returns>FieldValue</returns>
    public static T GetPrivateFieldValue<T>(this object obj, string propName)
    {
        if (obj == null) throw new ArgumentNullException("obj");
        Type t = obj.GetType();
        FieldInfo fi = null;
        while (fi == null && t != null)
        {
            fi = t.GetField(propName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
            t = t.BaseType;
        }
        if (fi == null)
            throw new ArgumentOutOfRangeException("propName",
                                                  string.Format("Field {0} was not found in Type {1}", propName,
                                                                obj.GetType().FullName));
        return (T) fi.GetValue(obj);
    }

    /// <summary>
    /// Sets a _private_ Property Value from a given Object. Uses Reflection.
    /// Throws a ArgumentOutOfRangeException if the Property is not found.
    /// </summary>
    /// <typeparam name="T">Type of the Property</typeparam>
    /// <param name="obj">Object from where the Property Value is set</param>
    /// <param name="propName">Propertyname as string.</param>
    /// <param name="val">Value to set.</param>
    /// <returns>PropertyValue</returns>
    public static void SetPrivatePropertyValue<T>(this object obj, string propName, T val)
    {
        Type t = obj.GetType();
        if (t.GetProperty(propName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance) == null)
            throw new ArgumentOutOfRangeException("propName",
                                                  string.Format("Property {0} was not found in Type {1}", propName,
                                                                obj.GetType().FullName));
        t.InvokeMember(propName,
                       BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.SetProperty |
                       BindingFlags.Instance, null, obj, new object[] {val});
    }


    /// <summary>
    /// Set a private Field Value on a given Object. Uses Reflection.
    /// </summary>
    /// <typeparam name="T">Type of the Field</typeparam>
    /// <param name="obj">Object from where the Property Value is returned</param>
    /// <param name="propName">Field name as string.</param>
    /// <param name="val">the value to set</param>
    /// <exception cref="ArgumentOutOfRangeException">if the Property is not found</exception>
    public static void SetPrivateFieldValue<T>(this object obj, string propName, T val)
    {
        if (obj == null) throw new ArgumentNullException("obj");
        Type t = obj.GetType();
        FieldInfo fi = null;
        while (fi == null && t != null)
        {
            fi = t.GetField(propName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
            t = t.BaseType;
        }
        if (fi == null)
            throw new ArgumentOutOfRangeException("propName",
                                                  string.Format("Field {0} was not found in Type {1}", propName,
                                                                obj.GetType().FullName));
        fi.SetValue(obj, val);
    }
}

关于c# - 我需要访问组合框的非公共(public)成员(突出显示的项目),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5031288/

相关文章:

c# - 在列表中查找 int 的索引

c# - 取消选择组合框项目

wpf - CheckBox 的默认控制模板

c# - WPF 中的编号列表

c# - 10 秒后重定向到新的 wpf 窗口?

c# - 设置 RadioButton 的 Checked 属性后,选项卡索引不起作用

c# - .NET c# - 自定义显示属性

c# - CheckedListBox 数据绑定(bind)到项目的选中状态

user-interface - 自动完成与下拉菜单。什么时候使用?

combobox - 带有 TPL 问题的 ExtJS 组合框