【发布时间】:2015-05-19 22:23:44
【问题描述】:
我目前正在开发一个 WPF 应用程序,它使用 Extended WPF Toolkit 库中的 PropertyGrid。
为了以独立于语言的方式显示名称和描述,我使用了一个包装类,该类包含根据提供的文档所需的属性。这是一个简短的列表
[LocalizedDisplayName("ServerConfig", typeof(Resources.Strings.Resources))]
public class ServerConfigPropertyGrid
{
#region fields
private ServerConfig serverConfig;
#endregion
#region properties
[LocalizedCategory("VeinStoreCategory", typeof(Resources.Strings.Resources))]
[LocalizedDisplayName("ActiveDirectoryPasswordDisplayName", typeof(Resources.Strings.Resources))]
[LocalizedDescription("ActiveDirectoryPasswordDescription", typeof(Resources.Strings.Resources))]
[Editor(typeof(PasswordEditor), typeof(PasswordEditor))]
public string LdapPassword
{
get { return serverConfig.LdapPassword; }
set { serverConfig.LdapPassword = value; }
}
#endregion
#region constructor
public ServerConfigPropertyGrid(ServerConfig serverConfig)
{
// store serverConfig
this.serverConfig = serverConfig;
}
#endregion
}
现在我想为 LdapPassword 属性使用自定义编辑器,因为它是一个密码,不应在 PropertyGrid 中以纯文本形式显示。由于我不是唯一一个也不是第一个提出这个要求的人,我在 Codeplex 项目的discussions section 中找到了这样一个编辑器的实现。
public class PasswordEditor : ITypeEditor
{
#region fields
PropertyItem _propertyItem;
PasswordBox _passwordBox;
#endregion
public FrameworkElement ResolveEditor(PropertyItem propertyItem)
{
_propertyItem = propertyItem;
_passwordBox = new PasswordBox();
_passwordBox.Password = (string)propertyItem.Value;
_passwordBox.LostFocus += OnLostFocus;
return _passwordBox;
}
private void OnLostFocus(object sender, RoutedEventArgs e)
{
// prevent event from bubbeling
e.Handled = true;
if (!_passwordBox.Password.Equals((string)_propertyItem.Value))
{
_propertyItem.Value = _passwordBox.Password;
}
}
}
根据 Codeplex 上提供的文档,所有需要做的就是在属性上添加 EditorAttribute,一切都应该没问题,但 PasswordEditor 根本没有显示。甚至没有调用构造函数,所以我认为 EditorAttribute 声明一定有问题。
我目前在配置为在 .NET 4.5.1 中编译的项目中使用扩展 WPF 工具包的版本 2.4.0。
有人知道可能出了什么问题?
【问题讨论】:
-
您使用的是最新的 WpfToolkit 版本(2.4.0)吗?哪个库“本地化”属性属于?我尝试在示例项目中使用
EditorAttribute,它可以正常工作。 -
感谢您回答 Il Vic。我正在通过 NuGet 管理我的依赖项,因此我使用的是 2.4.0 版和扩展的 WPF 工具包。 “本地化...”属性是自制的,因此它们目前处于相同的解决方案中。它们也可以工作,因为字符串在 PropertyGrid 中正确显示。您是否还使用包装类来保存您的属性,或者您是否使用了其他类设计?
标签: c# wpf propertygrid