mjk6026
mjk6026

Reputation: 1484

If i added a lot of controls in StackPanel, the UI Virtualization is applied?

I have a some question about UI Virtualizing in StackPanel.

<ScrollViewer>
    <StackPanel Orientation="Vertical">
        <!--item1.-->
        <StackPanel Orientation="Horizontal" Margin="5">
            <Button/> 
            <TextBlock Text="oh hi."/>
        </StackPanel>        

        <!--item2.-->
        <StackPanel Orientation="Horizontal" Margin="5">
            <Button/> 
            <TextBlock Text="oh hi."/>
        </StackPanel>        

        <!--item3.-->
        <StackPanel Orientation="Horizontal" Margin="5">
            <Button/> 
            <TextBlock Text="oh hi."/>
        </StackPanel>        

        <!--item4.-->
        <StackPanel Orientation="Horizontal" Margin="5">
            <Button/> 
            <TextBlock Text="oh hi."/>
        </StackPanel>        

        ...


        <!--item9999.-->
        <StackPanel Orientation="Horizontal" Margin="5">
            <Button/> 
            <TextBlock Text="oh hi."/>
        </StackPanel>        

    </StackPanel>
</ScrollViewer>

I heared about WPF UI Virtualization.

and then, If I add a lot of controls in StackPanel, A UI Virtualization works automatic at this StackPanel?

I know StackPanel.VirtualizingStackPanel.IsVirtualizing has setted to True by default.

Upvotes: 0

Views: 590

Answers (1)

John Gardner
John Gardner

Reputation: 25106

There's nothing "virtual" about your example here, though. you've explicitly created 9999 items inside your stack panel.

Virtualization is when some other itemscontrol (like a list, tree, grid) has a virtualizing panel inside of it, and the items control is generating/removing/reusing items as necessary to make it appear that the stack panel has 9999 items in it, when it really has only a few.

the simplest comparison to your above would be a ListBox control, with an ItemsSource of a list of 9999 items in it, and a DataTemplate:

<ListBox ItemsSource="{Binding Path=TheListOf9999Items}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal" Margin="5">  
                <Button/>   
                <TextBlock Text="oh hi."/>  
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

the listbox internally would have a virtualizing stack panel (its ItemsPanel) which would then generate items as necessary as you scroll up and down.

Upvotes: 1

Related Questions