public Control CallAddProductControl(string productScanCode, WarehouseContext dbcontext, Action<Product> createCallback = null)
        {
            var formControl = FormControlHelper.CreateFormControl();
            var product = new Product();
            product.ScanCode = productScanCode;
            formControl.RenderForm(product, false);
            formControl.ConfirmButton.Content = "录入产品";
            formControl.SubmitCallback = (d) =>
            {
                try
                {
                    dbcontext.Products.Add((Product)d);
                    dbcontext.SaveChanges();
                    if (createCallback != null)
                        createCallback((Product)d);
                }
                catch (Exception ex)
                {
                    //TODO: handle exception
                    formControl.SetErrorMsgManually("ScanCode", "此产品扫码已存在");
                }
            };

            return formControl;
        }
        public Control CallCreateUserControl(Action<User> createCallback=null)
        {
            var formControl = FormControlHelper.CreateFormControl();
            formControl.RenderForm(new User(),false);
            formControl.ConfirmButton.Content = "创建用户";
            formControl.SubmitCallback = (d) =>
            {
                using (var dbcontext = new WarehouseContext())
                {
                    try
                    {
                        dbcontext.Users.Add((User)d);
                        dbcontext.SaveChanges();
                        if (createCallback != null)
                            createCallback((User)d);
                    }
                    catch (Exception ex)
                    {
                        //TODO: handle exception
                        formControl.SetErrorMsgManually("Username", "此用户名已存在,请更换");
                    }
                }
            };

            return formControl;
        }
 private void DeleteUser_Clicked(object sender, RoutedEventArgs e)
 {
     var data = ((Button)sender).DataContext as User;
     if(data!=null){
         var result = MessageBox.Show(
             string.Format("你是否决定要删除用户'{0}'?", data.Name),
             "操作警告",
             MessageBoxButton.OKCancel,
             MessageBoxImage.Warning);
         if (result == MessageBoxResult.OK)
         {
             try
             {
                 using (var dbcontext = new WarehouseContext())
                 {
                     var deluser = dbcontext.Users.Find(((User)data).UserId);
                     dbcontext.Users.Remove(deluser);
                     dbcontext.SaveChanges();
                     edit_br.Child = null;
                     ReloadUserGrid();
                 }
             }
             catch (Exception ex)
             {
                 WindowMgr.SendNotification("删除操作失败。", NotificationLevel.Error);
             }
         }
     }
 }
 public static void SaveMetadata(string name, JObject jo)
 {
     using (var dbcontext = new WarehouseContext())
     {
         var data = dbcontext.Metadatas.FirstOrDefault(
             m => string.Equals(m.Name, name));
         if (data != null)
         {
             data.Content = jo.ToString();
             dbcontext.SaveChanges();
         }
         else
         {
             var newmeta = new Warehouse.DataModel.Entities.Metadata
             {
                 Name = name,
                 ContentType = "text/json",
                 Content = jo.ToString()
             };
             dbcontext.Metadatas.Add(newmeta);
             dbcontext.SaveChanges();
         }
     }
 }
 void ResetPassword()
 {
     using (var dbcontext = new WarehouseContext())
     {
         var updateuser = dbcontext.Users.Find(UpdateUser.UserId);
         if (updateuser != null)
         {
             updateuser.Password = pwdbox1.Password;
         }
         dbcontext.SaveChanges();
     }
 }
        public Control CallUpdateUserControl(User user, Action<User> updateCallback=null)
        {
            var formControl = FormControlHelper.CreateFormControl();
            // remove password
            formControl.DetermineFieldCreationCallback = (cx, s) =>
            {
                switch (cx.PropertyInfo.Name)
                {
                    case "Username":
                        return true;
                    default:
                        return s;
                }
            };

            formControl.CreateControlCallback = (cx, c) =>
            {
                if (cx.ControlType == ControlType.Editable)
                {
                    if (cx.PropertyInfo.Name == "Password")
                    {
                        return CreateResetPwdButton(user);
                    }
                    else if (cx.PropertyInfo.Name == "Username")
                    {
                        c.IsEnabled = false;
                    }
                }
                return c;
            };

            formControl.SubmitCallback = (d) =>
            {
                using (var dbcontext = new WarehouseContext())
                {
                    try
                    {
                        var selectuser = d as User;
                        var updateuser = dbcontext.Users.Find(selectuser.UserId);
                        updateuser.Name = selectuser.Name;
                        updateuser.PhoneNumber = selectuser.PhoneNumber;
                        updateuser.Memo = selectuser.Memo;
                        updateuser.IdentificationNumber = selectuser.IdentificationNumber;
                        updateuser.PermissionGroup = selectuser.PermissionGroup;
                        updateuser.Department = selectuser.Department;
                        dbcontext.SaveChanges();
                        WindowMgr.SendNotification("更新用户成功", NotificationLevel.Information);
                        if (updateCallback != null)
                            updateCallback(selectuser);
                    }
                    catch (Exception ex)
                    {
                        // TODO
                        WindowMgr.SendNotification("更新用户失败",NotificationLevel.Error);
                    }
                }
            };

            formControl.RenderForm(user, false);
            formControl.ConfirmButton.Content = "保存设置";

            return formControl;
        }