简体   繁体   English

绑定字典 <string, double> XAML数字文本块WPF的条目

[英]Bind Dictionary<string, double> entries to a XAML number textblock WPF

In my XAML file, I have a 3rd party (syncfusion) DoubleTextBox control: 在我的XAML文件中,我有一个第三方(syncfusion)DoubleTextBox控件:

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

When the DoubleTextBox loses focus, it copies the value to a Dictionary that I have created (the XAML element and the dictionary key have the exact same name), in order to save the keys and values to use on another page. 当DoubleTextBox失去焦点时,它将值复制到我创建的Dictionary(XAML元素和Dictionary键具有完全相同的名称),以便保存要在另一页上使用的键和值。

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

When a DoubleTextBox loses focus (this has been set in styles): 当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                 
            }               
        }
    }

All of this works as expected. 所有这些都按预期工作。 I am also able to save the globalDictionary using JSON serialization to a text file, using the SaveFileDialog, and then open the file again and de-serialize it. 我还能够使用JSON序列化使用SaveFileDialog将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);
        }
}

Open File: 打开文件:

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
        }
}

Deserialize JSON String 反序列化JSON字符串

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

All the values from the saved file are successfully written back to the globalDictionary. 已保存文件中的所有值都已成功写回到globalDictionary。

My question now is, after the file is open and the contents read, is there an easy way to put those contents back into the number text box through automatically iterating through each XAML element? 现在我的问题是,在打开文件并读取内容之后,是否有一种简单的方法可以通过自动迭代每个XAML元素将这些内容放回数字文本框中? The user should automatically see that all the values have been restored. 用户应自动看到所有值都已还原。

The brute force way is to do the following after opening the file. 蛮力方式是在打开文件后执行以下操作。 If possible, I'd prefer not to do it, since I have over 100 elements. 如果可能的话,我最好不要这样做,因为我有100多个元素。

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

Any alternate suggestions will be appreciated. 任何其他建议将不胜感激。

Why not put your 100+ DoubleTextBox controls inside Listbox and define these DoubleTextBox as Data Template? 为什么不将100多个DoubleTextBox控件放入Listbox并将这些DoubleTextBox定义为数据模板? Such as : 如 :

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

And then set binding to your dictionary in the code after load: 然后在加载后在代码中设置对字典的绑定:

myListBox.ItemsSource=App.globalDictionary; 
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;
         }
     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM