WPF 的自定义设计时属性

发布于 2024-08-26 01:53:29 字数 1505 浏览 4 评论 0原文

我已经创建了一个自定义控件,并且想要创建一个提供下拉列表或组合框的属性(在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

囚你心 2024-09-02 01:53:30

您可能正在寻找 PropertyValueEditor。

下面是演练:实现内联值编辑器

You are probably looking for the PropertyValueEditor.

Here's a Walkthrough: Implementing an Inline Value Editor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文