我正在尝试创建一个登录屏幕,但我想通过代码完成所有操作。
因此通过 C# 生成完整的 UI。
但是我正在使用 MVVM 模型,并且我的用户名上有数据绑定,但这不适用于标准密码框,所以我在网上找到了一个命名空间来解决这个问题,它被称为
PasswordBoxAssistant
来自FunctionalFun.UI。
在 xaml 中,我可以使用助手创建一个密码框,但我通过代码制作 UI,但我无法找到如何使用 PasswordBoxAssistant。
所以我有这个:
并且想要同样的东西,但是在我的 C# 中:
但是就像你在我的代码中看到的那样,我无权访问它。
不知道 xaml 是否只能在所有内容都已编译的情况下访问它,或者为什么我不能在我的 C# 中使用它。
有人可以帮助我实现这个或为我指明方向吗?
PasswordBoxAssistant 似乎是一个类似于 PassWordHelper 的附加属性:
https://gist.github.com/alamsal/fcefce4fb2d2d70fb2d91ee12741a35f
您可以按照此处所述附加此类属性:
因此类似
PasswordBoxAssistant.SetBindPassword(yourPasswordBox, true);
将其添加到变量 yourPasswordBox 中。
当然,您需要在 BoundPassword 依赖属性上创建绑定。
您绑定为目标的属性是PasswordBoxAssistant 依赖属性。
代码中的绑定有点复杂。我很少使用这种方法。
一个粗略的例子:
Binding myBinding = new Binding();
// myBinding.Source = ViewModel; Datacontext is default, you probably don't need source.
myBinding.Path = new PropertyPath("SomeStringPropertyInDatacontext");
myBinding.Mode = BindingMode.TwoWay;
myBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
yourPasswordBox.SetBinding(PasswordBoxAssistant.BoundPasswordProperty, myBinding);