Edit dialog, with bindings and OK/Cancel in WPF

Posted by Erik on Stack Overflow See other posts from Stack Overflow or by Erik
Published on 2010-04-01T10:53:59Z Indexed on 2010/04/01 17:43 UTC
Read the original article Hit count: 524

Filed under:
|

How can i have a dialog for editing the properties of a class with binding, and have OK-Cancel in the dialog?

My first idea was this:

public partial class EditServerDialog : Window {
    private NewsServer _newsServer;

    public EditServerDialog(NewsServer newsServer) {
        InitializeComponent();

        this.DataContext = (_newsServer = newsServer).Clone();
    }

    private void ButtonClick(object sender, RoutedEventArgs e)
    {
        switch (((Button)e.OriginalSource).Content.ToString()) {
            case "OK":
                _newsServer = (NewsServer)this.DataContext;
                this.Close();
                break;
            case "Cancel":
                this.Close();
                break;
        }
    }
}

When in the switch, case "OK", the DataContext contains the correct information, but the originally passed NewsServer instance does not change.

© Stack Overflow or respective owner

Related posts about wpf

Related posts about databinding