Share via


Don't shoot the messenger [A WebBrowserExtensions workaround for Windows Phone and a BestFitPanel tweak for infinite layout bounds on Windows Phone/Silverlight/WPF]

**

This blog has moved to a new location and comments have been disabled.

All old posts, new posts, and comments can be found on The blog of dlaa.me.

See you there!

Comments

  • Anonymous
    April 26, 2011
    Hi David, I am a Silverlight developer and I frequently read your blog.  Your recent posts on the "BestFitPanel" were very interesting.  I studied your code and I have implemented a bestfitpanel for a very unique task.  I am building a garden designer in Silverlight and I created a panel that will automatically fill a square area with evenly spaced same size rectangles.  The rectangle's FILL will be populated from an image of a plant.  This allows my users to see the same plant evenly spaced in a given area of their yard.  So, using the MeasureOverride and ArrangeOverride I am able to automatically add plants so that the entire square is filled with evenly spaced plants. Now I want to take it one step further, but, I've run into a fundamental problem.  I am hoping you might be able to point me in the right direction.  Imagine that you want to plant flowers in an even pattern inside a circle.  As I mentioned above, doing this in a square is easy with the concepts you laid out in the bestfitpanel.  So, I added a circular shape to my panel and the circle's height and width match the height and width of the panel.  As the panel is resized, the circle is resized to match the panel's height and width.  I then add enough rectangles to fill the panel in an evenly spaced pattern.  Before I call child.Arrange to position the uielement (rectangle in this case) in the correct row and column (rect), I'm trying to determine if the uielement (rectangle) will be inside or outside the circle shape.  If the row/column is outside the circle shape I don't want to show a plant there, so, I won't want to call child.Arrange for that row/column.  I can use transformtovisual, findelementsinhostcoordinates to get the coordinates of a given point and find out what uielements are at that point (i.e. to see if the circle shape is at a given point).  The fundamental problem I have run into is that when ArrangeOverride is happening the panel's layout is being created (for lack of a better term).  For some reason transformtovisual and findelementsinhostcoordinates don't work because the layout seems to be in flux or in progress.  If I do transformtovisual, findelementsinhostcoordinates from a mouse click then I can easily determine whether a shape (rectangle) is above the circle or not.   I hope I haven't rambled on, but, as you can see this is a difficult problem.  I have scoured the web and I have tried many different approaches.  But, I have not been able to solve this problem.  In summary, when I call child.Arrange in the bestfitpanel I want to be able to see if the shape will be put inside or outside a circle shape.  If outside the shape I don't want to arrange it there.  I only want to arrange inside the circle shape.  Unfortunately, I'm not sure ArrangeOverride is the best place to do all this because the final layout is not complete.  For some reason the methods transformtovisual, findelementsinhostcoordinates do not seem to return accurate information at that point.  Whereas, if i use transformtovisual, findelementsinhostcoordinates inside a mouseleftbuttondown event then the detection of the circle shape is flawless. I appreciate any ideas or suggestions you can make and I appreciate you excellent blog posts on Silverlight. Best regards, Chris

  • Anonymous
    April 26, 2011
    Chris, Trying to query the layout system for information during a layout can be tricky even at the best of times. :) I think that's what you're running into here. My quick recommendation would be to do the math yourself without calling into TransformToVisual, etc. - fortunately, a circle seems pretty easy to deal with for what you're doing here. But my more platform-correct answer is to warn you that what you're doing seems a little unusual - it's not very common for a layout container to fake-show/hide its children like this. What you're doing makes a certain amount of sense, but I'm just warning that it may lead you into other strange behaviors because of its non-standardness. At any rate, it sounds pretty neat! :)

  • Anonymous
    April 28, 2011
    Hi David, Just wanted to say thanks for your advice.  I got the bestfitpanel to display my uielements in a circle.  Now I'm working on ellipses and then triangles!!! ... I took your advice and researched the math behind it.  Great advice! Best regards, Chris

  • Anonymous
    April 28, 2011
    Chris, Great news - glad to hear it! :)

  • Anonymous
    May 01, 2011
    Hi David, Here is another update ... I got both circle and ellipse to work.  Yes, it is all possible through math!  Best advice ever.  Thanks so much. Best regards, Chris