如何通过combobox.items迭代字典的键

发布于 2025-02-11 00:21:58 字数 1821 浏览 1 评论 0原文

我的应用程序具有以下方法,该方法以前有效,但是在重构为多个组件后不再起作用。

/// <summary>
/// Attempt to set the combobox to the item with the supplied string
/// </summary>
protected void SetComboBoxSelection(ComboBox cmb, string ItemText)
{
     if (cmb.DisplayMember.ToLower() == "key" && cmb.ValueMember.ToLower() == "value")
     {
          foreach (KeyValuePair<string, object> item in cmb.Items)
          {
               if (item.Key == ItemText || ((string)item.Key).HasPatternMatch(ItemText))
               {
                    cmb.SelectedItem = item;
                    return;
               }
          }     
     }
}

我得到以下例外:

System.InvalidCastException: 'Unable to cast object of type 'System.Collections.Generic.KeyValuePair`2[System.String,NetworkSyncObjects.NetworkFolder]' to type 'System.Collections.Generic.KeyValuePair`2[System.String,System.Object]'.'

该方法用于设置各种组合的选择,每种组合都具有不同的字典类型,但所有这些都以“键”为字符串类型。现在,由于非常无用的答案,“这是不允许的。

该代码再次起作用。但是现在事实并非如此。我知道,由于列表中的项目的假定铸造,它显然无法正常工作。 (重复的答案是关于将列表本身施放,而不是列表中的项目)。

这是我尝试

  • 使用动态项目 - &gt;不编译。
  • 使用keyvaluepair&lt; string,dynamic&gt; - &gt;编译但
  • 使用var obj作为Dynamic/var obj作为keyValuePair&lt; string,object&gt; - &gt;两者总是导致null obj

问题:

我如何修改此方法与任何combobox一起使用,其项目库为字典&lt; string,t&gt;,其中t是多种类型的对象)?

For example, some will be: Dictionary, Dictionary, Dictionary, <代码>字典&lt; string,int&gt;

据我所知,除非您抛出列表如果不明确召集KeyValuePair中的确切类型,就不允许使用。再一次,不知道为什么这起作用了,现在不知道。但是我在这里。

My application has the following method, which previously worked, but after refactoring into multiple assemblies no longer works.

/// <summary>
/// Attempt to set the combobox to the item with the supplied string
/// </summary>
protected void SetComboBoxSelection(ComboBox cmb, string ItemText)
{
     if (cmb.DisplayMember.ToLower() == "key" && cmb.ValueMember.ToLower() == "value")
     {
          foreach (KeyValuePair<string, object> item in cmb.Items)
          {
               if (item.Key == ItemText || ((string)item.Key).HasPatternMatch(ItemText))
               {
                    cmb.SelectedItem = item;
                    return;
               }
          }     
     }
}

I get the following exception:

System.InvalidCastException: 'Unable to cast object of type 'System.Collections.Generic.KeyValuePair`2[System.String,NetworkSyncObjects.NetworkFolder]' to type 'System.Collections.Generic.KeyValuePair`2[System.String,System.Object]'.'

This method is being used to set the selection of various comboboxes, each with a different dictionary type, but all of which has the 'key' as a string type. Now, my previous question was closed due to the very unhelpful answer of (paraphrasing) "thats not allowed. heres an duplicate question that only sort of explains why".

Once again, this code WORKED. But now it doesn't. I understand that it its apparently not working due to the supposed invalid casting of the items within the list. (The duplicate answer was about casting the list itself, not the items within the list).

Here is what I've tried

  • using dynamic item -> doesn't compile.
  • using KeyValuePair<string,dynamic> -> compiles but gets an identical runtime exception as noted above
  • using the var obj as dynamic / var obj as KeyValuePair<string,object> -> both always result in a null obj

Question:

How can I modify this method to work with any combobox whose itemsource is a Dictionary<string,T> where T is various types of objects)?

For example, some will be: Dictionary<string,NetworkFolder>, Dictionary<string,FileInfo>, Dictionary<string,DirectoryInfo>, Dictionary<string,int>

As far as I can tell, there is no way to access the Key portion of the KeyValuePair within the list unless you cast the list, which is apparently not allowed without explicitly calling out the exact type in the keyvaluepair. Once again, no idea why this was working and now isnt. But here I am.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ま柒月 2025-02-18 00:21:58

更新的方法,使用idictionary接口并直接访问数据源绑定是我解决的方式。

从这篇文章中修改: https://stackoverflow.com/a/555850559/12135042

protected void SetComboBoxSelection(ComboBox cmb, string ItemText)
        {
            if (cmb.DisplayMember.ToLower() == "key" && cmb.ValueMember.ToLower() == "value")
            {
                IDictionary Items = (IDictionary)((BindingSource)cmb.DataSource).DataSource;
                int i = 0;
                foreach (string item in Items.Keys)
                {
                    if (item == ItemText || item.HasPatternMatch(ItemText))
                    {
                        cmb.SelectedIndex = i;
                        return;
                    }
                    i++;
                }
                    
            }
        }

Updated Method, using the IDictionary interface and directly accessing the datasource binding was how I resolved it.

Modified from this post: https://stackoverflow.com/a/55850559/12135042

protected void SetComboBoxSelection(ComboBox cmb, string ItemText)
        {
            if (cmb.DisplayMember.ToLower() == "key" && cmb.ValueMember.ToLower() == "value")
            {
                IDictionary Items = (IDictionary)((BindingSource)cmb.DataSource).DataSource;
                int i = 0;
                foreach (string item in Items.Keys)
                {
                    if (item == ItemText || item.HasPatternMatch(ItemText))
                    {
                        cmb.SelectedIndex = i;
                        return;
                    }
                    i++;
                }
                    
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文