/// <summary>
        ///
        /// </summary>
        private void PermissionTarget_dialog_ApplyingChanges(object sender, CancelRoutedEventArgs e)
        {
            // Retrieve relevant parameters
            object[] parameters = PermissionTarget_dialog.Content as object[];
            Oltp.AccountPermissionDataTable permissionsTable = parameters[1] as Oltp.AccountPermissionDataTable;

            DataTable changes = permissionsTable.GetChanges();

            // No changes were made, skip the apply (but don't cancel)
            if (changes == null)
            {
                e.Skip = true;
                PermissionTarget_dialog.EndApplyChanges(e);
                return;
            }

            // Save the changes to the DB
            Window.AsyncOperation(delegate()
            {
                using (OltpProxy proxy = new OltpProxy())
                {
                    proxy.Service.AccountPermission_Save(changes as Oltp.AccountPermissionDataTable);
                }
            },
                                  delegate(Exception ex)
            {
                // Failed, so cancel and display a message
                MainWindow.MessageBoxError("Error while saving permissions.", ex);
                e.Cancel = true;
                return(false);
            },
                                  delegate()
            {
                permissionsTable.AcceptChanges();
                PermissionTarget_dialog.EndApplyChanges(e);
            });
        }
        /// <summary>
        /// Open the dialog
        /// </summary>
        private void PermissionTarget_dialog_Open(object sender, RoutedEventArgs e)
        {
            // Set dataItem as current item
            ListViewItem currentItem = _listTable.GetParentListViewItem(e.OriginalSource as FrameworkElement);

            Oltp.UserRow      user  = currentItem.Content as Oltp.UserRow;
            Oltp.UserGroupRow group = currentItem.Content as Oltp.UserGroupRow;

            // Retrieve applied permissions from the server
            Oltp.AccountPermissionDataTable permissionsTable = null;
            Window.AsyncOperation(delegate()
            {
                using (OltpProxy proxy = new OltpProxy())
                {
                    permissionsTable = proxy.Service.AccountPermission_Get(Window.CurrentAccount.ID,
                                                                           user == null ? group.ID : user.ID,
                                                                           user == null
                                                                           );
                }
            },
                                  delegate(Exception ex)
            {
                MainWindow.MessageBoxError("Failed to open permissions.", ex);
                return(false);
            },
                                  delegate()
            {
                _openingDialog = true;

                // Apply retrieved permissions to the GUI radio buttons
                ListBox permissionsList = VisualTree.GetChild <ListBox>(PermissionTarget_dialog);
                for (int i = 0; i < permissionsList.Items.Count; i++)
                {
                    ApiMenuItem x          = (ApiMenuItem)permissionsList.Items[i];
                    string permissionValue = x.Path;
                    if (permissionValue == null)
                    {
                        continue;
                    }

                    Oltp.AccountPermissionRow perm = permissionsTable.FindByAccountIDTargetIDTargetIsGroupPermissionType(
                        Window.CurrentAccount.ID,
                        user == null ? group.ID : user.ID,
                        user == null,
                        permissionValue);

                    ListBoxItem lbItem = (ListBoxItem)permissionsList.ItemContainerGenerator.ContainerFromIndex(i);

                    if (perm == null)
                    {
                        VisualTree.GetChild <RadioButton>(lbItem, "radioNotSet").IsChecked = true;
                    }
                    else if (perm.Value)
                    {
                        VisualTree.GetChild <RadioButton>(lbItem, "radioAllow").IsChecked = true;
                    }
                    else
                    {
                        VisualTree.GetChild <RadioButton>(lbItem, "radioDeny").IsChecked = true;
                    }
                }

                currentItem.IsSelected = true;

                // Show the dialog
                PermissionTarget_dialog.Title = String.Format("Editing permissions for {0}", user == null ? group.Name : user.Name);
                PermissionTarget_dialog.BeginEdit(new object[] { user == null ? group as DataRow : user as DataRow, permissionsTable });

                _openingDialog = false;
            });
        }