【发布时间】:2016-11-01 01:27:33
【问题描述】:
我正在尝试为 WF 重新托管设计器解决方案中使用的 WPF PropertyGrid 构建密码(屏蔽)字段。请注意,我对它的安全元素不感兴趣。我只想对用户隐藏密码。我真的为一些我认为很容易实现的东西而苦苦挣扎,但我最终发现这篇很棒的文章真的很有帮助:
WPF PasswordBox and Data binding
根据文章创建 PasswordBoxAssistant 类后,我使用以下 XAML 和代码创建了 WPF UserControl:
XAML
<Grid>
<PasswordBox x:Name="PasswordBox"
Background="Green"
local:PasswordBoxAssistant.BindPassword="true"
local:PasswordBoxAssistant.BoundPassword="{Binding Password,
Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
</Grid>
代码隐藏:
public partial class PasswordUserControl : UserControl
{
public PasswordUserControl()
{
InitializeComponent();
}
public static readonly DependencyProperty PasswordProperty =
DependencyProperty.Register("Password",
typeof(string), typeof(PasswordUserControl),
new FrameworkPropertyMetadata(PasswordChanged));
public string Password
{
get
{
return (string)GetValue(PasswordProperty);
}
set
{
SetValue(PasswordProperty, value);
}
}
private static void PasswordChanged(DependencyObject source,
DependencyPropertyChangedEventArgs e)
{
if (e.NewValue != null)
{
(source as PasswordUserControl)?.UpdatePassword(e.NewValue.ToString());
}
else
{
(source as PasswordUserControl)?.UpdatePassword(string.Empty);
}
}
private void UpdatePassword(string newText)
{
PasswordBox.Password = Password;
}
}
然后我创建了一个PropertyValueEditor 类(称为PasswordEditor.cs),我假设这是不工作的部分,我没有正确设置。
我将 PasswordProperty 从 UserControl 绑定到 PropertyValueEditor 的 Value 字段。
public class PasswordEditor : PropertyValueEditor
{
public PasswordEditor()
{
this.InlineEditorTemplate = new DataTemplate();
FrameworkElementFactory stack = new FrameworkElementFactory(typeof(StackPanel));
FrameworkElementFactory passwordBox = new
FrameworkElementFactory(typeof(PasswordUserControl));
Binding passwordBoxBinding = new Binding("Value") { Mode = BindingMode.TwoWay };
passwordBox.SetValue(PasswordUserControl.PasswordProperty, passwordBoxBinding);
stack.AppendChild(passwordBox);
this.InlineEditorTemplate.VisualTree = stack;
}
}
我已尝试将其设置为 StringValue 以及我发现的其他 WF PropertyValueEditor 示例,但无济于事。
密码(屏蔽)字段现在在我的 WPF PropertyGrid 中正确显示,但当我切换到另一个 WF 活动并切换回包含密码字段的活动时,它不会保留该值。
谁能指出我正确的方向?
谢谢。
再次感谢任何帮助。
谢谢。
【问题讨论】:
标签: c# wpf data-binding workflow-foundation-4 propertygrid