开发者

Customized design-time attribute for WPF

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 "Comm开发者_如何学Con 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.


You are probably looking for the PropertyValueEditor.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜