How to retrieve data from a dialog box?

Posted by Ralph on Stack Overflow See other posts from Stack Overflow or by Ralph
Published on 2011-01-12T07:17:09Z Indexed on 2011/01/12 7:53 UTC
Read the original article Hit count: 158

Filed under:
|

Just trying to figure out an easy way to either pass or share some data between the main window and a dialog box.

I've got a collection of variables in my main window that I want to pass to a dialog box so that they can be edited.

They way I've done it now, is I pass in the list to the constructor of the dialog box:

private void Button_Click(object sender, RoutedEventArgs e)
{
    var window = new VariablesWindow(_templateVariables);
    window.Owner = this;
    window.ShowDialog();
    if(window.DialogResult == true) 
        _templateVariables = new List<Variable>(window.Variables);
}

And then in there, I guess I need to deep-copy the list,

public partial class VariablesWindow : Window
{
    public ObservableCollection<Variable> Variables { get; set; }

    public VariablesWindow(IEnumerable<Variable> vars)
    {
        Variables = new ObservableCollection<Variable>(vars);
        // ...

So that when they're edited, it doesn't get reflected back in the main window until the user actually hits "Save".

Is that the correct approach? If so, is there an easy way to deep-copy an ObservableCollection? Because as it stands now, I think my Variables are being modified because it's only doing a shallow-copy.

© Stack Overflow or respective owner

Related posts about c#

Related posts about wpf