locked
How to make a UniformGrid scrollable? RRS feed

  • Question

  • hi,

    In my Silverlight player, I programmatically generate a fluctuating number of buttons and put them into a UniformGrid, it works fine. But the out-most container, say, a Grid, is size-limited. Buttons will be squeezed if there are too many of them need to be fit in.

    So I wrap a ScrollViewer outside of the UniformGrid, planning to make the UniformGrid scrollable if necessary. But it seems not working, buttons are still squeezed together.

    Any one knows how to do it?  thanks!

    Peter 

    Thursday, September 11, 2014 7:54 AM

Answers

  • I would try removing any height and width you have set on the UniformGrid and size the things going into it instead.

    I would expect the grid to size to the contents and your scrollviewer should kick in if the UniformGrid won't fit in the window.

    When I look at the code here

    http://www.jeff.wilcox.name/2009/01/uniform-grid/

    I see comments which indicate the thing seems to be measuring contents size:

            /// <summary>
            /// Compute the desired size of the UniformGrid by measuring all of the
            /// children with a constraint equal to a cell's portion of the given
            /// constraint. The maximum child width and maximum child height are
            /// tracked, and then the desired size is computed by multiplying these
            /// maximums by the row and column count.
            /// </summary>
            /// <param name="constraint">The size constraint.</param>
            /// <returns>Returns the desired size.</returns>


    Hope that helps
    Please don't forget to up vote answers you like or which help you and mark one(s) which answer your question.

    • Marked as answer by litway Wednesday, September 17, 2014 11:14 AM
    Thursday, September 11, 2014 12:46 PM
    Moderator

All replies

  • I would try removing any height and width you have set on the UniformGrid and size the things going into it instead.

    I would expect the grid to size to the contents and your scrollviewer should kick in if the UniformGrid won't fit in the window.

    When I look at the code here

    http://www.jeff.wilcox.name/2009/01/uniform-grid/

    I see comments which indicate the thing seems to be measuring contents size:

            /// <summary>
            /// Compute the desired size of the UniformGrid by measuring all of the
            /// children with a constraint equal to a cell's portion of the given
            /// constraint. The maximum child width and maximum child height are
            /// tracked, and then the desired size is computed by multiplying these
            /// maximums by the row and column count.
            /// </summary>
            /// <param name="constraint">The size constraint.</param>
            /// <returns>Returns the desired size.</returns>


    Hope that helps
    Please don't forget to up vote answers you like or which help you and mark one(s) which answer your question.

    • Marked as answer by litway Wednesday, September 17, 2014 11:14 AM
    Thursday, September 11, 2014 12:46 PM
    Moderator
  • Hi,

    The UniformGrid do not support scrolling. It is just a panel that layout your items.

    You could try to use additional code in order to implement this. First you need to insert the Panel into a ScrollViewer to enable scrolling. Then you can see the offset of the row you are interested by using the UIElement.TransformToVisual method. When you have the offset you can set the VerticalOffset property of the ScrollViewer to scroll to the item.

    Besides, link below could give you some help.

    http://msdn.microsoft.com/en-us/library/system.windows.controls.scrollviewer(v=vs.95).aspx

    Best Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.




    Monday, September 15, 2014 2:30 AM
  • Thank you Andy,

    removing the width and height constraint of UniformGrid did the trick!

    Wednesday, September 17, 2014 11:17 AM