locked
What is the best way to create a reuseable ResizeableBorder? RRS feed

  • Question

  • Hi all,

    Functions:
    - Has the same functions as the standard Border
    - Has a thumb which allows the user to resize it
    - Fires SizeChangedByUser event if the user changes the size by dragging the thumb
    - Usage must be similar with Border:
    <ResizeableBorder>
    .....
    </ResizeableBorder>


    Seems a lot of ways to do that, but what is the best way?
    1) Directly extend the Border class?
    2) Use a control template? (Code behind is needed because of the event stuff, so it cannot be put in a ResourceDictionary)
    3) Create a custom control?
    4) Create a user Control?

    Best regards,
    Zach
    Wednesday, November 4, 2009 3:36 AM

Answers

  • Border does not inherit from Control, so it does not have a ControlTemplate. But you could inherit from it if the default style is good enough and all you need is some resizing adorner. More info on adorners here:

    The content-like behavior also indicates that a ContentControl might be a good place to start if you want to go for a custom control. You can give it a new controltemplate with a Border and a ContentPresenter inside. And you can probably use a similar adorner as above for the resizing. You will also have to add some dependency properties to mimic the ones from the Border element.

    A user control is not the right option in my opinion, because I only use this if I want to group a couple of controls together in a reusable way. This is just extending the features of a single existing control.

    Personally I think I would first try the first option, because I think it's cleaner, but I'm not sure if you can manipulate the border's size without some Microsoft code getting in the way ;-)

    hth,
    Marcel

    http://dutchmarcel.wordpress.com/
    • Marked as answer by Linda Liu Tuesday, November 10, 2009 4:00 AM
    Wednesday, November 4, 2009 8:11 AM
  • Hi Zach,

    Creating a UserControl isn't a solution to your problem because we can't add a FrameworkElement into a UserControl as a child at all.

    Defining a custom ControlTemplate isn't a solution because Border is not Control. Even if Border is Control, besides defining a custom ControlTemplate for the Border class in order to include a Thumb in the visual tree, we still need to derive from the Border class so as to add code to implement the resizing function. In addition, it's not convenient for developers to reuse the ResizableBorder control in this way. They would need to include the custom control template in multiple places in their application.

    The solution of creating a custom control requires you to implement both the functionality and the ControlTemplate, which requires much efforts.

    As Marcel has replied, you can derive from the ContentControl class and in the ControlTemplate of the derived control, include a Border and Thumb. The shortcoming of this solution is that developers could replace the default control template of your control, so the Thumb may be excluded.

    A good solution is to apply a adorner to the Border class. Implement a resizable adorner and bind this adorner to the derived Border class. Developers can't exclude the resizable adorner by defining their own ControlTemplate for the ResizableBorder because a adorner is placed in the adorner layer.

    Hope this helps.

    Sincerely,
    Linda Liu
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Linda Liu Tuesday, November 10, 2009 4:00 AM
    Saturday, November 7, 2009 5:07 PM

All replies

  • Border does not inherit from Control, so it does not have a ControlTemplate. But you could inherit from it if the default style is good enough and all you need is some resizing adorner. More info on adorners here:

    The content-like behavior also indicates that a ContentControl might be a good place to start if you want to go for a custom control. You can give it a new controltemplate with a Border and a ContentPresenter inside. And you can probably use a similar adorner as above for the resizing. You will also have to add some dependency properties to mimic the ones from the Border element.

    A user control is not the right option in my opinion, because I only use this if I want to group a couple of controls together in a reusable way. This is just extending the features of a single existing control.

    Personally I think I would first try the first option, because I think it's cleaner, but I'm not sure if you can manipulate the border's size without some Microsoft code getting in the way ;-)

    hth,
    Marcel

    http://dutchmarcel.wordpress.com/
    • Marked as answer by Linda Liu Tuesday, November 10, 2009 4:00 AM
    Wednesday, November 4, 2009 8:11 AM
  • Hi Zach,

    Creating a UserControl isn't a solution to your problem because we can't add a FrameworkElement into a UserControl as a child at all.

    Defining a custom ControlTemplate isn't a solution because Border is not Control. Even if Border is Control, besides defining a custom ControlTemplate for the Border class in order to include a Thumb in the visual tree, we still need to derive from the Border class so as to add code to implement the resizing function. In addition, it's not convenient for developers to reuse the ResizableBorder control in this way. They would need to include the custom control template in multiple places in their application.

    The solution of creating a custom control requires you to implement both the functionality and the ControlTemplate, which requires much efforts.

    As Marcel has replied, you can derive from the ContentControl class and in the ControlTemplate of the derived control, include a Border and Thumb. The shortcoming of this solution is that developers could replace the default control template of your control, so the Thumb may be excluded.

    A good solution is to apply a adorner to the Border class. Implement a resizable adorner and bind this adorner to the derived Border class. Developers can't exclude the resizable adorner by defining their own ControlTemplate for the ResizableBorder because a adorner is placed in the adorner layer.

    Hope this helps.

    Sincerely,
    Linda Liu
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Linda Liu Tuesday, November 10, 2009 4:00 AM
    Saturday, November 7, 2009 5:07 PM