【问题标题】:Bind Dictionary<string, double> entries to a XAML number textblock WPF将 Dictionary<string, double> 条目绑定到 XAML 数字文本块 WPF
【发布时间】:2023-04-09 12:29:01
【问题描述】:

在我的 XAML 文件中,我有一个第 3 方(同步融合)DoubleTextBox 控件:

<syncfusion:DoubleTextBox x:Name="personAHeight" Style="{StaticResource SFDoubleTB}" />

当 DoubleTextBox 失去焦点时,它会将值复制到我创建的字典(XAML 元素和字典键具有完全相同的名称),以便保存键和值以在另一个页面上使用。

public static Dictionary<string, double> globalDictionary = new Dictionary<string, double>()
{
        {"personAHeight", 0}, {"personBHeight", 0},
        // over 100 keys & values
}

当 DoubleTextBox 失去焦点时(这已在样式中设置):

void SFTextBox_LostFocus(object sender, RoutedEventArgs e)
    {
        var tb1 = sender as DoubleTextBox;

        if ((double) tb1.Value != 0)
        {
            if (App.globalDictionary.ContainsKey(tb1.Name))
            {
                App.globalDictionary[tb1.Name] = (double) tb1.Value;              // always replace the value                  
            }
            else
            {
                App.globalDictionary.Add(tb1.Name, (double) tb1.Value);           // add the entry to the dictionary in app.xaml.cs                 
            }               
        }
    }

所有这些都按预期工作。我还可以使用 SaveFileDialog 将使用 JSON 序列化的 globalDictionary 保存到文本文件中,然后再次打开该文件并对其进行反序列化。

private void saveProject_Click(object sender, RoutedEventArgs e)
{
        SaveFileDialog saveFileDialog = new SaveFileDialog();

        if(saveFileDialog.ShowDialog() == true)
        {                                 
            string jsonString = App.SerializeToJSONString();
            File.WriteAllText(saveFileDialog.FileName, jsonString);
        }
}

打开文件:

private void openProject_Click(object sender, RoutedEventArgs e)
{
        OpenFileDialog openFileDialog = new OpenFileDialog();
        if (openFileDialog.ShowDialog() == true)
        {
            string stringToDeserialize = File.ReadAllText(openFileDialog.FileName);
            App.DeserializeJSONString(stringToDeserialize);

           // read this data and then put the value back into XAML number box
        }
}

反序列化 JSON 字符串

public static void DeserializeJSONString(string jsonString)
    {
        App.globalDictionary = JsonConvert.DeserializeObject<Dictionary<string, double>>(jsonString);
    }

已保存文件中的所有值都已成功写回 globalDictionary。

我现在的问题是,在打开文件并读取内容后,是否有一种简单的方法可以通过自动迭代每个 XAML 元素将这些内容放回数字文本框中? 用户应该会自动看到所有值都已恢复。

蛮力方法是打开文件后执行以下操作。如果可能的话,我宁愿不这样做,因为我有超过 100 个元素。

personAHeight.Value = App.globalDictionary["personAHeight"];
...repeat x 100

我们将不胜感激任何替代建议。

【问题讨论】:

    标签: c# json wpf xaml dictionary


    【解决方案1】:

    为什么不将您的 100 多个 DoubleTextBox 控件放入 Listbox 并将这些 DoubleTextBox 定义为数据模板?如:

     <ListBox x:Name="myListBox">
            <ListBox.ItemTemplate>
                <WrapPanel>
                <Label Content={Binding Key}
                <syncfusion:DoubleTextBox Style="{StaticResource SFDoubleTB}" Value={Binding Value}/>
                </WrapPanel>
            </ListBox.ItemTemplate>
        </ListBox>
    

    然后在加载后在代码中设置绑定到你的字典:

    myListBox.ItemsSource=App.globalDictionary; 
    

    【讨论】:

    • 我认为列表框不适合我的目的,因为所有变量都需要用户输入值,另一个页面将执行计算并显示结果。我的应用程序基本上只是进行计算,我还希望从设计角度来看它是分开的。
    • 列表框不会限制您输入值,它只会简化您的硬编码设计,以根据提供的字典自动动态创建控件
    【解决方案2】:
    public void RestoreValues()
    {   
        var controlContainer = VisualTreeHelper.GetChild(???, ???) as FrameworkElement;
        if (controlContainer != null)
        {
            foreach (var keyValuePair in App.globalDictionary)
            {
                var doubleTextBox = controlContainer.FindName(keyValuePair.key) as syncfusion:DoubleTextBox;
                if (doubleTextBox != null)
                    doubleTextBox.Value = keyValuePair.Value;
             }
         }
    }
    

    【讨论】:

    • 谢谢,您的回答让我想到了如何在一个框架中找到所有孩子。我有很多堆栈面板和网格元素,我在下面的链接中找到了我的答案:stackoverflow.com/questions/14875042/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多