使用 propdp 代码段创建依赖项属性时,它不会为您提供正确的类名称,您可以在其中创建依赖项属性,并且您可以像下一个示例一样手动输入:
namespace YourApp.Controls
{
public sealed class YourButton : Control
{
public YourButton()
{
this.DefaultStyleKey = typeof(YourButton);
}
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
// Using a DependencyProperty as the backing store for Text. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(ownerclass), new PropertyMetadata(0));
}
}
我不想作为默认值所有者类,在这种情况下我想要 YourButton 。
如何修改代码段以提出正确的名称?
答案 0 :(得分:1)
分析 ctor 代码段的源代码,它很容易知道问题:您只需要添加下一行:
<Function>ClassName()</Function>
在文字所有者类的定义中。
文件必须如下:
...
<Literal>
<ID>ownerclass</ID>
<ToolTip>The owning class of this Property. Typically the class that it is declared in.</ToolTip>
<Function>ClassName()</Function>
<Default>ownerclass</Default>
</Literal>
...
然后你默认拥有你想要的东西:
namespace YourApp.Controls
{
public sealed class YourButton : Control
{
public YourButton()
{
this.DefaultStyleKey = typeof(YourButton);
}
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
// Using a DependencyProperty as the backing store for Text. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(YourButton), new PropertyMetadata(0));
}
}
使用Why "propdp" code snippet doesn't use the nameof operator for the name of the registered property?中建议的修改,使用 nameof 运算符。