【发布时间】:2011-11-01 23:14:18
【问题描述】:
我已经创建了一个自定义控件,并且想创建一个属性(在 Blend 的设计时可用),它可以提供一个下拉菜单或组合框。然后设计者将选择可用选项之一。非常像“通用属性”选项卡中的“光标”组合,除了我想完全控制组合中的项目。选择可能会有所不同,因此我不能使用硬编码的“枚举”。
我知道可以像这样声明设计属性:
protected string mString;
[Category("Common Properties")]
[DisplayName("My Friendly Name")]
public string MyFriendlyName
{
get { return mString; }
set { mString= value; }
}
在上面的例子中,“我的友好名称”只是一个字符串。用户可以输入任何他想要的内容。
protected Uri mPathname;
[Category("Common Properties")]
[DisplayName("Resource pathname")]
public Uri MyResPathname
{
get { return mPathname; }
set { mPathname = value; }
}
在上述情况下,“资源路径名”有一个组合框,但项目列表由 Blend 处理。
如果我使用枚举,结果是一个包含我的项目的组合,但我无法更改项目列表。
public enum MyChoices
{
Aaa,
Bbb
}
public class MyButton : Button
{
(...)
[Category("Common Properties")]
public MyChoices MyChoice
{
get { return (MyChoices)GetValue(MyChoiceProperty); }
set { SetValue(MyChoiceProperty, value); }
}
public static readonly DependencyProperty MyChoiceProperty =
DependencyProperty.Register("MyChoice",
typeof(MyChoices),
typeof(MyButton ),
new UIPropertyMetadata(
(MyChoices)MyChoices.Aaa,
OnMyChoiceChangedCallback));
}
在上面的例子中,选择是硬编码在枚举中的......
谁能帮忙?我敢肯定这很容易,我已经很接近了,但现在我要绕圈子了。
【问题讨论】:
标签: wpf attributes design-time