Custom Panel which Inherits Panel RRS feed

  • Question

  • I want to make my own Panel  but i dont know how i can do that. I read in MSDN that i should override the methods MeasureOverride & Arrange Override. Can anyone give me steps to understand how i do this and how can i add additional interaction?

    For example I want my panel to give the user the opportunity to rearrange the components with drag and drop and change their position with a Scroller.
    Tuesday, January 5, 2010 8:48 AM


  • Hi I wrote a quick tutorial on how to do it few days ago. If you want to check it out is here:
    with the full source code at the end

    just add the C# class I provided into your project, change the namespace if needed and build your
    solution. If does not appear into your assets, close expression blend and reopen it :)

    It does not shows how to manage drag and drop, but it's pretty straight forward:

    1) override method drag enter and provide feedback on wether or not the data can be dropped
    2) override method drop to analyze data dropped

            protected override void OnDragEnter(DragEventArgs e)
                string format = "Text";
                if (!e.Data.GetDataPresent(format))
                    //unwanted data, so provide visual feedback
                    e.Effects = DragDropEffects.None;
                else {
                    e.Effects = DragDropEffects.Copy;

            protected override void OnDrop(DragEventArgs e)
                FrameworkElement element = e.Data.GetData("FrameworkElement") as FrameworkElement;
                //add the element to the Panel
                //call the custom refresh (view) method to re-arrange all elements


    the Refresh method
    private void Refresh()
         foreach(FrameworkElement element in this.Children)
               //..perform checks and re-arrangements

               //update the element with the new arrangements
               element.Arrange(new Rect(xPos, yPos, myWidth, myHeight));

    I hope this will help!
    • Marked as answer by Angelica_gr Friday, January 8, 2010 10:55 AM
    Tuesday, January 5, 2010 2:55 PM