WPF 的自定义设计时属性
我已经创建了一个自定义控件,并且想要创建一个提供下拉列表或组合框的属性(在 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));
}
在上面的示例中,选择被硬编码在枚举中......
任何人都可以帮忙吗?我确信这很容易,我已经很接近了,但现在我却在原地踏步。
I've created a custom control, and would like to create an attribute (available in Blend's design-time) which would offer a dropdown or combobox. The designer would then select one of the available options. Very much like the "Cursor" combo in the "Common Properties" tab, except that I want full control over what items go in the combo. The choices can vary, so I can't use a hard-coded "enum".
I know it's possible to declare design attributes like this:
protected string mString;
[Category("Common Properties")]
[DisplayName("My Friendly Name")]
public string MyFriendlyName
{
get { return mString; }
set { mString= value; }
}
In the case above, "My Friendly Name" is just a string. The user can enter whatever he wants.
protected Uri mPathname;
[Category("Common Properties")]
[DisplayName("Resource pathname")]
public Uri MyResPathname
{
get { return mPathname; }
set { mPathname = value; }
}
In the case above, "Resource pathname" has a combo box, but the list of items are handled by Blend.
If I use an enum, the result is a combo with my items in it, but then I can't change the item-list.
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));
}
In the example above, the choices are hard-coded in the enum...
Can anyone help ? I'm sure it's easy, I'm very close but now I'm going in circles.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能正在寻找 PropertyValueEditor。
下面是演练:实现内联值编辑器。
You are probably looking for the PropertyValueEditor.
Here's a Walkthrough: Implementing an Inline Value Editor.