简体   繁体   English

在WPF中,为什么ListBox VirtualizingStackPanel在Windows XP上不起作用?

[英]In WPF, why doesn't a ListBox VirtualizingStackPanel work on Windows XP?

For performance reasons, I need to use virtualization on a scrolling list box. 出于性能原因,我需要在滚动列表框中使用虚拟化。

My XAML: 我的XAML:

<Grid>
    <StackPanel>
        <Button Content="Fill" Click="Button_Click" />
        <ListBox ItemsSource="{Binding People}" ScrollViewer.CanContentScroll="True" VirtualizingStackPanel.IsVirtualizing="True">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <VirtualizingStackPanel>
                        <TextBox Text="{Binding FirstName}" />
                    </VirtualizingStackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </StackPanel>
</Grid>

My Code-behind: 我的后台代码:

public MainWindow()
{
    InitializeComponent();

    DataContext = _mainViewModel;
}

MainViewModel _mainViewModel = new MainViewModel();

private void Button_Click(object sender, RoutedEventArgs e)
{
    _mainViewModel.FillPeople();
}

My ViewModel: 我的ViewModel:

class MainViewModel : INotifyPropertyChanged
{
    public ObservableCollection<Person> People { get; set; }

    public MainViewModel()
    {
        People = new ObservableCollection<Person>();
    }

    public void FillPeople()
    {
        for (int i = 0; i < 100; i++)
        {
            var person = new Person { FirstName = "John" };
            People.Add(person);
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

My Data Class: 我的资料类别:

class Person
{
    public string FirstName { get; set; }
}

When I run this code on Windows 7 and Windows 10, the list fills out quickly and correctly. 当我在Windows 7和Windows 10上运行此代码时,该列表会快速正确地填写。 When I run it on POSReady 2009 SP3 (a variant of Windows XP) the list box is blank. 在POSReady 2009 SP3(Windows XP的变体)上运行它时,列表框为空白。 Using a regular StackPanel fixes it, but I need virtualization. 使用常规的StackPanel可以解决此问题,但是我需要虚拟化。 Does anyone know what to do to get this working on XP? 有谁知道该怎么做才能在XP上运行?

...that's not how you use a VirtualizingStackPanel . ...这不是您使用VirtualizingStackPanel You are putting each individual TextBox in its own VirtualizingStackPanel . 您将每个单独的TextBox放入其自己的VirtualizingStackPanel ListBox is virtualizing by default though, so you shouldn't need to do anything. 不过, 默认情况下ListBox正在虚拟化 ,因此您无需执行任何操作。

MSDN: MSDN:

For the ListBox, the default ItemsPanelTemplate specifies the VirtualizingStackPanel. 对于ListBox,默认的ItemsPanelTemplate指定VirtualizingStackPanel。 For MenuItem, the default uses WrapPanel. 对于MenuItem,默认使用WrapPanel。 For StatusBar, the default uses DockPanel. 对于StatusBar,默认使用DockPanel。

Get rid of those extra panels and see if it starts working on XP again. 摆脱掉那些多余的面板,看看它是否再次可以在XP上运行。

Let's pretend that ListBox didn't virtualize by default though. 让我们假设ListBox在默认情况下没有进行虚拟化。 The correct way to make it virtualize would be like this (note, that I'm setting the ItemsPanelTemplate , not the ItemTemplate ): 使它虚拟化的正确方法是这样的(请注意,我正在设置ItemsPanelTemplate ,而不是ItemTemplate ):

<ListBox>
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel IsVirtualizing="True" />
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
</LitBox>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM