【问题标题】:Customized design-time attribute for WPFWPF 的自定义设计时属性
【发布时间】: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


    【解决方案1】:

    您可能正在寻找 PropertyValueEditor。

    这是Walkthrough: Implementing an Inline Value Editor

    【讨论】:

    • 我试过了,它和 Visual Studio 所宣传的一样完美,但在 Blend (Microsoft Expression) 中却不行。我仍在阅读相关文章,以了解如何在 Expression Blend 中做同样的事情。如果您有答案(您似乎拥有所有答案!)无论如何,请告诉我,非常感谢您的帮助。我越来越近了……
    • 我对 Blend 没有太多经验,但我会检查程序集是否实际加载(如果您在同一个项目中有控件,也将它们移动到单独的程序集)。
    • 这比我想象的要复杂。但是,我发现了一篇关于这个主题的非常可靠的文章(我现在正在阅读它)。在这里,如果它对任何人有用:blogs.silverlight.net/blogs/justinangel/archive/2008/11/17/…
    猜你喜欢
    • 2021-08-15
    • 2018-01-27
    • 1970-01-01
    • 1970-01-01
    • 2010-10-15
    • 2013-04-13
    • 1970-01-01
    • 2011-01-11
    • 2011-09-03
    相关资源
    最近更新 更多