Search code examples
vb.netsilverlightuser-controlsstackpanelwrappanel

Hide objects/User Controls within WrapPanel?


I've got a WrapPanel which will contain several different custom UserControls. Depending on the scenario, I may need to filter down which UserControls are visible. My goal is that I can switch which controls are visible on the fly by showing/hiding the controls that need to be filtered - thus shifting the controls that are left showing, to the top-left of the panel.

Right now I am simply setting the Visibility property of the control to Visibility.Collapsed when I don't want them to appear. I thought that because I was using a WrapPanel, the rest of the controls would shift to the top-left of the panel.

Instead, after hiding some of the UserControls, the controls that are still visible stay exactly where they were before, and I am left with gaps between the controls that are still showing. I've opened my app in Silverlight Spy, and it shows that the UserControls are still actually there (which makes sense) but are simply invisible.

So my question is: Is there a way that I can show/hide UserControls within a WrapPanel which allows the still-visible UserControls to slide to their new positions (all shifting towards the top left - similar to a StackPanel)?

I've debated removing the UserControls completely from the WrapPanel (I think this would work) and storing them in memory until they are needed. Then if I wanted to show/hide other controls, I would get them from my in-memory object. It seems like there should be a better way to do this though.

If anyone has any suggestions or advice, it would be greatly appreciated. Thanks!

-Lloyd

UPDATE:

XAML: (very simple)

<toolkit:WrapPanel x:Name="MyLayout" Height="300" HorizontalAlignment="Left" VerticalAlignment="Top" Width="400" />

Code-Behind: The UserControls are getting added dynamically:

MyLayout.Children.Add(oUserControl)

And they are getting set to collapsed dynamically as well:

oUserControl.Visibility = Visibility.Collapsed


Solution

  • I think I've found the problem. We added the WrapPanel to a ScrollViewer recently, and when I took the ScrollViewer out I was able to achieve the functionality I wanted.

    I'm not sure why the ScrollViewer would have that effect though?

    Also, I've found that I can leave the ScrollViewer in place and simply call .Measure() on the WrapPanel to update the layout.

    Neither option makes 100% sense to me, but they do both seem to work.