以前に、ビジュアル ツリーをたどって各項目を XML ファイルに保存することで、これに取り組みました。XML レイアウトはビジュアル ツリーを反映します。ツリーをリロードすると、XML ファイルをウォークしてデフォルト値をロードできます。
private void ForEachControlRecursive(object root, Action<Control> action, bool IsRead)
{
Control control = root as Control;
//if (control != null)
// action(control);
// Process control
ProcessControl(control, IsRead);
// Check child controls
if (root is DependencyObject && CanWeCheckChildControls(control))
foreach (object child in LogicalTreeHelper.GetChildren((DependencyObject)root))
ForEachControlRecursive(child, action, IsRead);
}
ProcessControl
基本的に、コントロールの種類ごとにスイッチがあり、特定のコントロールのカスタマイズされた機能にルーティングされます。
例えば:
private void ProcessControl(TextBox textbox, bool IsRead)
{
//1. textbox.Name; - Control name
//2. Text - Control property
//3. textbox.Text - Control value
if (IsRead)
{
// Class that reads the XML file saving the state of the visual elements
textbox.Text = LogicStatePreserver.GetValue(textbox).ToString();
}
else
{
LogicStatePreserver.UpdateControlValue(textbox, textbox.Text);
}
}