Edit dialog, with bindings and OK/Cancel in WPF
- by Erik
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.