WPF: copying of XmlElement
I create a window in WPF. The constructor takes a "ref XmlElement settings" which is used to display information in that window.
In the constructor, I clone those settings:
_ClonedSettings = (XmlElement)settings.Clone();
and I keep a pointer to the original settings:
_OriginalSettings = settings;
The UI modifies _ClonedSettings开发者_如何学Python
. Then if user hits OK button, I want to overwrite the original settings with the _ClonedSettings
, so the window creator gets the right values.
How do I do this final copying operation?
Don't use a ref. Just set the settings as a public property on the window
public class SettingsEditor : Window
{
public XmlElement Settings {get;set;}
/*...*/
}
Within the Window, update the settings and such as you've done. Once the window has been shown, get the settings out.
/*...*/
var editor = new SettingsEditor { Settings = settings };
editor.ShowDialog();
settings = editor.Settings;
/*...*/
You can also set a DialogResult on your window to see what happened with the editor...
精彩评论