Joe Wood's profileMeasured and ArrangedBlogLists Tools Help
    September 03

    Gotchas with Custom Controls in WPF

    Here's a few lessons learnt whilst developing WPF usercontrols, custom controls and custom ItemsControls.

    Failed to recognize Dependency Property or Attached Property.

    Make sure that the static property is defined for on the class it is being used.  For a dependency property called 'Custom' with a default value of 1.0:

          public static DependencyProperty CustomProperty = DependencyProperty.Register("Custom", typeof(int),
                    typeof(MyClass), new PropertyMetadata( 0, new PropertyChangedCallback(Callback)));

    Also make sure that the class level accessors have been added:

    public int Custom
    {
    get { return (int)GetValue(CustomProperty); }
         set { SetValue(CustomProperty,value); }
    }

    For Attached Properties use RegisterAttached:       

    public static DependencyProperty CustomProperty = DependencyProperty.RegisterAttached("Custom", typeof(int),
                    typeof(MyClass), new PropertyMetadata( 0, new PropertyChangedCallback(Callback)));

    The public methods :

           public static void SetCustom(UIElement element, int value)
           {
                element.SetValue(CustomProperty, value);
           }
           public static int GetCustom(UIElement element)
           {
                return (int)element.GetValue(CustomProperty);
           }

    Error Loading XAML after adding a Dependency/Attached Property

    Check that datatype of the default value.  Make sure that it matches the type of the property (1.0 for a double versus 1 - which is an integer).

    Implicit Styles not being picked up for the custom control:

    When defining a style with a key of the data type of the custom control all the settings are ignored.

    First, check that the default resource key has been defined in the custom control.  Add the following public static constructor, this indicates that the default style for this control should use the Type object of MyClass :

            static MyClass()
            {
                DefaultStyleKeyProperty.OverrideMetadata(typeof(MyClass),
                    new FrameworkPropertyMetadata(typeof(MyClass)));
            }

    Also, do not set the Style property of the custom control in the XAML definition.  To apply a default style use a local style definition with a key of the type object.

    Your Custom Control is not being created as the item in the ItemsControl

    To instruct an ItemsControl derived class to create a specific custom control for databound items you need to override two virtuals:

            protected override DependencyObject GetContainerForItemOverride()
            {
                return new MyClass();
           }
            protected override bool IsItemItsOwnContainerOverride(object item)
           {
               return (item is MyClass);
            }
     

    Comments

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Trackbacks

    The trackback URL for this entry is:
    http://joewood.spaces.live.com/blog/cns!ED8F6AE13739FF99!160.trak
    Weblogs that reference this entry
    • None