Question

I have created a list with ScrollViewer and stackpanel. I am adding user control to this stackpanel from codebehind.
I want to virtualize data that I can improve the performance of my application.
How can i achieve this?

I cann't use Listbox because I am adding user control as DataItems and each user control have a different width and height.. Please suggest how to implement that Code:

XAML

<ScrollViewer>
    <StackPanel x:Name="stckPnlComponentsLst" Visibility="{Binding IsBusy, Converter={StaticResource BooleanToVisibilityInvertedConverter}}" Orientation="Vertical">
    </StackPanel>
</ScrollViewer>

C#

for (int count = 0; count < countItems; count++)
{
    stckPnlComponentsLst.Children.Add(new ChannelNewsLstControl(ViewModel.ComponentData[count], false, false));
}
Était-ce utile?

La solution 2

Use a ListBox instead. Check the sample here. For example, define ListBox in a xaml

<ListBox x:Name="MyList">

and then in codebehind

var MyListData = new List<ChannelNewsLstControl>();
MyListData.Add(new ChannelNewsLstControl {name = "MyFirstChannelName});
MyList.ItemsSource=MyListData;

If you have really lots of items, i'd recommend to use Telerik's DataBoundListBox as it is much faster than a normal ListBox (at least, for wp7) and supports virtualization and async loading. Sample would be pretty the same, as component is inherited ListBox and adding own features mentioned before.

EDIT: final answer:

Try to put a grid inside of your ItemTemplate and set its RowDefinition.Height = "Auto". See some details here

Autres conseils

I don't see any case in which using a StackPanel inside a ScrollViewer is a good idea. You should not do that.

There are 2 controls to do what you want, ListBox and ListView.

If you really want to stick with a stackpanel inside a ScrollViewer, just replace your StackPanel, by a VirtualizingStackPanel. But again, you should NOT be doing that.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top