我需要一个字典,该字典的Type为键和Control为值。例如对于字符串类型,控件将是TextBox;对于布尔类型,控件将是单选按钮,依此类推...... 问题是当我声明像Dictionary<Type, Control>
这样的字典时,并尝试添加TextBox,例如它说TextBox是一种类型,它在给定的背景。 任何想法?将System.Windows.Controls存储到字典中
0
A
回答
0
您必须声明Dictionary<Type, Type>
而不是Dictionary<Type, Control>
因为TextBox
是Type
:
private static Dictionary<Type, Type> s_ControlTypes = new Dictionary<Type, Type>() {
{typeof(string), typeof(TextBox)},
{typeof(bool), typeof(RadioButton)},
};
,然后用它
// Let's create a control for, say, `bool` type:
Control ctrl = Activator.CreateInstance(s_ControlTypes[typeof(bool)]) as Control;
0
Dictionary<Type, Control> dictionary = new Dictionary<Type, Control>();
dictionary.Add(typeof(string), textBox);
dictionary.Add(typeof(bool), checkBox);
0
看起来像你想添加Control
型(字面)到字典,它应该包含Control
实例:
var dictionary = new Dictionary<Type, Control>();
dictionary.Add(typeof(string), TextBox);
你不能这样做。你需要要么把特定实例的Control
,或者,如果这仅仅是一个有些地图进一步参考,重新申报词典:
var dictionary = new Dictionary<Type, Type>();
与类型填充:
dictionary.Add(typeof(string), typeof(TextBox));
// and so on
相关问题
- 1. 如何将字典值从字典数组中存储到数组中
- 2. 存储输出到字典中
- 3. 在Rhino.Mocks中存储字典
- 4. 读取文件并将内容存储到字典中 - Python
- 5. 字典存储对。谁储存三倍?
- 6. 字典不存储值
- 7. plist或字典存储
- 8. Python字典存储键值
- 9. 将文本从uitextfield存储到字典键
- 10. 如何使用字典将字符串数组存储到Realm实例中?
- 11. 将数字存储到MySQL
- 12. 在爱尔兰的字典中存储字典
- 13. 如何存储在字典中
- 14. 在会话中存储字典
- 15. 如何在字典中存储变量
- 16. 在字典中存储整数
- 17. 在字典中存储查询结果
- 18. python,在数据框中存储字典
- 19. 在字典中存储函数[Python]
- 20. Python,在数据库中存储字典
- 21. 在Json中存储字典的数据
- 22. 如何在文件中存储字典?
- 23. 在Elgg中存储键值字典
- 24. 在Python中将类的实例存储在字典中
- 25. 在隔离存储中存储字典<TKey,TValue>
- 26. 更新存储在cPickle中的字典而不将其加载到RAM中
- 27. 将数据存储到json字段中
- 28. 将字符串存储到数组中?
- 29. 将字典保存到文件
- 30. 将推文保存到python字典