locked
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

Answers

  • Hi I wrote a quick tutorial on how to do it few days ago. If you want to check it out is here:
    http://blogs.academicclub.org/uidev/?p=89
    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

    1)
            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 {
                    //success
                    e.Effects = DragDropEffects.Copy;
                    base.OnDragEnter(e);
                }
            }

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

                base.OnDrop(e);
            }

    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!
    -dave
    • Marked as answer by Angelica_gr Friday, January 8, 2010 10:55 AM
    Tuesday, January 5, 2010 2:55 PM