我需要一個字典,該字典的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. 將字典存儲到Firebase中?
- 3. 存儲字典到hashtable
- 4. 將代碼存儲在字典中
- 5. 如何將字典值從字典數組中存儲到數組中
- 6. 存儲輸出到字典中
- 7. 將文本字段數據存儲到字典中快速
- 8. 在字典中存儲lambdas
- 9. 在字典中存儲HttpResponse
- 10. 在Rhino.Mocks中存儲字典
- 11. Python字典存儲
- 12. 將對象存儲到Django的字典中
- 13. 讀取文件並將內容存儲到字典中 - Python
- 14. 字典存儲對。誰儲存三倍?
- 15. 將枚舉值保存到字典中
- 16. 如何將數據字典/表存儲在內存中?
- 17. plist或字典存儲
- 18. Numpy或Python存儲字典
- 19. 字典不存儲值
- 20. Python字典存儲鍵值
- 21. 存儲複雜的字典
- 22. 將Python字典轉儲到JSON文件
- 23. 將變量結果從while循環存儲到字典(python)
- 24. Julia將分區數據框存儲到字典
- 25. 將文本從uitextfield存儲到字典鍵
- 26. 將輸出存儲到列表或字典
- 27. 存儲數組中的枚舉VS存儲在字典中?
- 28. 如何使用字典將字符串數組存儲到Realm實例中?
- 29. 將數字存儲到MySQL
- 30. 在愛爾蘭的字典中存儲字典