public ResetPwdWindow(User user)
     : this()
 {
     UpdateUser = user;
     this.DataContext = user;
 }
        public static void AddSampleData()
        {
            using (var dbcontext = new WarehouseContext())
            {
                try
                {
                    var user1 = new User()
                    {
                        Name = "二娃",
                        Username = "******",
                        Password = "******",
                        IdentificationNumber = "310392198305114344",
                        PhoneNumber = "13543776409",
                        Company = "梁山",
                        Department = "库房管理部",
                        Memo = "(●'◡'●)"
                    };
                    dbcontext.Users.Add(user1);

                    var tg = new TagGroup()
                    {
                        Name = "功能"
                    };
                    dbcontext.TagGroups.Add(tg);

                    var tag1 = new Tag()
                    {
                        Name = "食品",
                        Group = tg
                    };
                    dbcontext.Tags.Add(tag1);

                    var tag2 = new Tag()
                    {
                        Name = "书籍",
                        Group = tg
                    };
                    dbcontext.Tags.Add(tag2);

                    var tag3 = new Tag()
                    {
                        Name = "酒水",
                        Group = tg
                    };
                    dbcontext.Tags.Add(tag3);

                    var product = new Product()
                    {
                        Name = "乐事薯片100g(清新黄瓜)",
                        ScanCode = "111"
                    };
                    dbcontext.Products.Add(product);

                    var good = new Goods()
                    {
                        GoodsCode = "111",
                        InboundDate = DateTime.Now,
                        Product = product,
                        State = GoodsState.Inbounding
                    };
                    dbcontext.Goods.Add(good);

                    dbcontext.SaveChanges();
                }
                catch
                {
                    // do nothing
                }
            }
            using (var dbcontext = new WarehouseContext())
            {
                var item = dbcontext.Goods.First();
            }
        }
        internal FrameworkElement RenderSelectUserControl(FormItemContext context, WarehouseContext dbcontext,
			User selecteduser = null)
        {
            var options = (from u in dbcontext.Users
                select new NameValuePair
                {
                    Name = u.Name,
                    Description = u.IdentificationNumber,
                    Value = u
                }).ToList();
            var ctl = new ComboBoxSink().CreateControlForLookup(context, options);

            if (selecteduser != null)
            {
                var selectedop = options.FirstOrDefault(o => ((User) o.Value).UserId == selecteduser.UserId);
                ((ComboBox) ctl).SelectedValue = selectedop.Value;
            }
            return ctl;
        }
        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;
        }
 FrameworkElement CreateResetPwdButton(User user)
 {
     var container = new StackPanel
     {
         Orientation = Orientation.Horizontal
     };
     var resetpwdBn = new Button
     {
         Content = "重置密码",
         Style = Application.Current.Resources["confirm_bn"] as Style
     };
     container.Children.Add(resetpwdBn);
     var msgtb = new TextBlock
     {
         Style = Application.Current.Resources["succeed_TextBlock"] as Style
     };
     container.Children.Add(msgtb);
     resetpwdBn.Click += (s, e) =>
     {
         CallResetPasswordPopup(user);
     };
     CustomValidation.SetValidationCallback(container, () => null);
     return container;
 }
 bool CallResetPasswordPopup(User user)
 {
     var pwdwindow=new ResetPwdWindow(user);
     var result = pwdwindow.ShowDialog();
     if (result.HasValue && result.Value)
     {
         WindowMgr.SendNotification("密码已重置", NotificationLevel.Information);
         return true;
     }
     return false;
 }
 public Control CallViewUserControl(User user)
 {
     var formControl = FormControlHelper.CreateFormControl();
     // remove password
     formControl.DetermineFieldCreationCallback = (cx, s) =>
     {
         switch (cx.PropertyInfo.Name)
         {
             case "Username":
                 return true;
             case "Password":
                 return false;
             default:
                 return s;
         }
     };
     formControl.RenderForm(user, true);
     return formControl;
 }
 private void ShowUserView(User user)
 {
     var usermgmt = new UserMgmtModule();
     var form = usermgmt.CallViewUserControl(user);
     edit_br.Child = form;
 }