【问题标题】:Is there anyway to have the key of a HashMap match up to a WPF tool name?无论如何,HashMap 的键是否与 WPF 工具名称相匹配?
【发布时间】:2021-02-04 19:14:24
【问题描述】:

标题措辞不太好,因为我不确定如何表达我的问题,但我正在 WPF C# 中创建一个应用程序,并且我正在使用 HashMaps 来保存和加载来自 Sqlite db 的数据。我已将我的 XAML 字段命名为与 SQLite db 中的列名称相同,并且我想在组件初始化时将这些值加载到我的应用程序的文本框中。

示例:(我目前如何拥有它,非常繁琐且似乎效率低下)

foreach (KeyValuePair<string, string> entry in dataToLoad)
            {
                switch (entry.Key)
                {
                    case "tsd_vac_on":
                        tsd_vac_on.Text = entry.Value;
                        break;

                    case "tsd_vac_off":
                        tsd_vac_off.Text = entry.Value;
                        break;
                }
            }

理想情况下,如果条目的键名已经与我要编辑的文本框的名称相同,我希望这样做。

foreach (KeyValuePair<string, string> entry in dataToLoad)
            {
                (entry.Key).Text = entry.Value;
            }
}

【问题讨论】:

  • ((TextBox)FindName(entry.Key)).Text = entry.Value;
  • @Clemens 就是这样!谢谢!

标签: c# .net wpf sqlite hashmap


【解决方案1】:

您可以使用FindName 方法访问命名元素,例如

if (FindName(entry.Key) is TextBox textBox)
{
    textBox.Text = entry.Value;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-20
    相关资源
    最近更新 更多