VirtualizingStackPanel 不适用于 ComboBox

发布于 2024-10-26 01:39:21 字数 6937 浏览 7 评论 0原文

我对 WPF 完全陌生。我不明白为什么 VirtualizingStackPanel 不能用于以下用途。我已经尝试过 ScrollViewer 但它对我不起作用。我尝试过将 VirtualizingStackPanel 放置在每个地方,但似乎没有任何影响。当组合框填充 3000 个项目并且每个项目具有 16x16 图像时,大约需要 30 秒才能显示。因此,任何帮助将不胜感激。

更新:数据模板已添加

<DataTemplate x:Key="ComboBoxItemTemplate">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="20"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" SharedSizeGroup="col1"/>
                <ColumnDefinition Width="Auto" SharedSizeGroup="col2"/>
                <ColumnDefinition Width="Auto" SharedSizeGroup="col3"/>
                <ColumnDefinition Width="Auto" SharedSizeGroup="col4"/>
                <ColumnDefinition Width="Auto" SharedSizeGroup="col5"/>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Image
            Width="16"
            Height="16"
            Margin="1"
            Source="{Binding Image}"/>
            <StackPanel Grid.Column="1" Margin="1">
                <TextBlock Text="{Binding DisplayName}"/>
            </StackPanel>
            <StackPanel Grid.Column="2" Margin="5,1,1,1">
                <TextBlock Text="{Binding Column2}"/>
            </StackPanel>
            <StackPanel Grid.Column="3" Margin="5,1,1,1">
                <TextBlock Text="{Binding Column3}"/>
            </StackPanel>
            <StackPanel Grid.Column="4" Margin="5,1,1,1">
                <TextBlock Text="{Binding Column4}"/>
            </StackPanel>
            <StackPanel Grid.Column="5" Margin="5,1,1,1">
                <TextBlock Text="{Binding Column5}"/>
            </StackPanel>
            <StackPanel Grid.Column="6" Margin="5,1,1,1">
                <TextBlock Text="{Binding InheritanceDescription}"/>
            </StackPanel>
        </Grid>
    </DataTemplate>

    <Style TargetType="{x:Type local:ComboBox}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:ComboBox}">
                    <Grid x:Name="LayoutRoot" Width="Auto" Height="Auto">
                        <Grid.Resources>
                            <Style x:Key="AlternatingRows" TargetType="{x:Type ComboBoxItem}">
                                <Setter Property="Width" Value="{Binding Path=Dropdownwidth, RelativeSource={RelativeSource AncestorType={x:Type local:ComboBox}}}" />
                                <Setter Property="Background" Value="#FFE7ECF5"/>
                                <Setter Property="UIElement.IsEnabled" Value="{Binding IsEnabled}"/>
                            </Style>
                        </Grid.Resources>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <Label
                            Grid.Column="0"
                            HorizontalAlignment="Left" HorizontalContentAlignment="Left"
                            Margin="4,2,4,2"
                            Visibility="{Binding Path=NameVisibility, RelativeSource={RelativeSource TemplatedParent}}"
                            Target="{Binding ElementName=combobox1}">
                            <AccessText Text="{Binding Path=DisplayName, Converter={StaticResource appendColon}, RelativeSource={RelativeSource TemplatedParent}}"/>
                        </Label>
                        <ComboBox
                         x:Name="combobox1"
                         IsEditable="False"
                         AlternationCount="2"
                         Width="{Binding Path=ChildWidth, RelativeSource={RelativeSource TemplatedParent}}"
                         Grid.Column="1"
                         Grid.IsSharedSizeScope="True"
                         HorizontalAlignment="Left"
                         ItemContainerStyle="{StaticResource AlternatingRows}"
                         Margin="0,2,4,2"
                         ItemTemplate="{StaticResource ComboBoxItemTemplate}"
                         SelectedItem="{Binding Path=SelectedItem, RelativeSource={RelativeSource TemplatedParent}}"
                         ToolTip="{Binding Path=Description, RelativeSource={RelativeSource TemplatedParent}}"
                         BorderBrush="#95B7F3">
                            <ComboBox.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <VirtualizingStackPanel VirtualizingStackPanel.IsVirtualizing="True"  VirtualizingStackPanel.VirtualizationMode="Recycling"/>
                                </ItemsPanelTemplate>
                            </ComboBox.ItemsPanel>
                        </ComboBox>
                        <StackPanel Grid.Column="2" Orientation="Horizontal" Width="Auto" Height="Auto">
                        <Label HorizontalAlignment="Left" HorizontalContentAlignment="Left" Margin="4,2,4,2" Height="Auto" Width="Auto"
                                Visibility="{Binding Path=UnitVisibility, RelativeSource={RelativeSource TemplatedParent}}"
                               >
                            <TextBlock Text="{Binding Path=DisplayUnit, RelativeSource={RelativeSource TemplatedParent}}"/>
                        </Label>
                        <Label Target="{Binding ElementName=combobox1}" HorizontalAlignment="Left" HorizontalContentAlignment="Left" Margin="4,2,4,2" Height="Auto" Width="Auto" MaxWidth="450"
                                Visibility="{Binding Path=DescriptionVisibility, RelativeSource={RelativeSource TemplatedParent}}"
                               >
                            <AccessText Text="{Binding Path=Description, RelativeSource={RelativeSource TemplatedParent}}" ToolTip="{Binding Path=Description, RelativeSource={RelativeSource TemplatedParent}}" TextTrimming="WordEllipsis" TextWrapping="Wrap"/>
                        </Label>
                        </StackPanel>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

I am absolutely new to WPF. I can't figure out why isn't the VirtualizingStackPanel working for the following. I already have experimented with the ScrollViewer but it doesnt' work for me. I have tried placing VirtualizingStackPanel every where but it seems to have no affect at all. The combobox when populated with 3000 item and each item has 16x16 image, takes around 30 seconds to show up. So any help will be appreciated.

Update: The datatemplate has been added

<DataTemplate x:Key="ComboBoxItemTemplate">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="20"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" SharedSizeGroup="col1"/>
                <ColumnDefinition Width="Auto" SharedSizeGroup="col2"/>
                <ColumnDefinition Width="Auto" SharedSizeGroup="col3"/>
                <ColumnDefinition Width="Auto" SharedSizeGroup="col4"/>
                <ColumnDefinition Width="Auto" SharedSizeGroup="col5"/>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Image
            Width="16"
            Height="16"
            Margin="1"
            Source="{Binding Image}"/>
            <StackPanel Grid.Column="1" Margin="1">
                <TextBlock Text="{Binding DisplayName}"/>
            </StackPanel>
            <StackPanel Grid.Column="2" Margin="5,1,1,1">
                <TextBlock Text="{Binding Column2}"/>
            </StackPanel>
            <StackPanel Grid.Column="3" Margin="5,1,1,1">
                <TextBlock Text="{Binding Column3}"/>
            </StackPanel>
            <StackPanel Grid.Column="4" Margin="5,1,1,1">
                <TextBlock Text="{Binding Column4}"/>
            </StackPanel>
            <StackPanel Grid.Column="5" Margin="5,1,1,1">
                <TextBlock Text="{Binding Column5}"/>
            </StackPanel>
            <StackPanel Grid.Column="6" Margin="5,1,1,1">
                <TextBlock Text="{Binding InheritanceDescription}"/>
            </StackPanel>
        </Grid>
    </DataTemplate>

    <Style TargetType="{x:Type local:ComboBox}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:ComboBox}">
                    <Grid x:Name="LayoutRoot" Width="Auto" Height="Auto">
                        <Grid.Resources>
                            <Style x:Key="AlternatingRows" TargetType="{x:Type ComboBoxItem}">
                                <Setter Property="Width" Value="{Binding Path=Dropdownwidth, RelativeSource={RelativeSource AncestorType={x:Type local:ComboBox}}}" />
                                <Setter Property="Background" Value="#FFE7ECF5"/>
                                <Setter Property="UIElement.IsEnabled" Value="{Binding IsEnabled}"/>
                            </Style>
                        </Grid.Resources>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <Label
                            Grid.Column="0"
                            HorizontalAlignment="Left" HorizontalContentAlignment="Left"
                            Margin="4,2,4,2"
                            Visibility="{Binding Path=NameVisibility, RelativeSource={RelativeSource TemplatedParent}}"
                            Target="{Binding ElementName=combobox1}">
                            <AccessText Text="{Binding Path=DisplayName, Converter={StaticResource appendColon}, RelativeSource={RelativeSource TemplatedParent}}"/>
                        </Label>
                        <ComboBox
                         x:Name="combobox1"
                         IsEditable="False"
                         AlternationCount="2"
                         Width="{Binding Path=ChildWidth, RelativeSource={RelativeSource TemplatedParent}}"
                         Grid.Column="1"
                         Grid.IsSharedSizeScope="True"
                         HorizontalAlignment="Left"
                         ItemContainerStyle="{StaticResource AlternatingRows}"
                         Margin="0,2,4,2"
                         ItemTemplate="{StaticResource ComboBoxItemTemplate}"
                         SelectedItem="{Binding Path=SelectedItem, RelativeSource={RelativeSource TemplatedParent}}"
                         ToolTip="{Binding Path=Description, RelativeSource={RelativeSource TemplatedParent}}"
                         BorderBrush="#95B7F3">
                            <ComboBox.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <VirtualizingStackPanel VirtualizingStackPanel.IsVirtualizing="True"  VirtualizingStackPanel.VirtualizationMode="Recycling"/>
                                </ItemsPanelTemplate>
                            </ComboBox.ItemsPanel>
                        </ComboBox>
                        <StackPanel Grid.Column="2" Orientation="Horizontal" Width="Auto" Height="Auto">
                        <Label HorizontalAlignment="Left" HorizontalContentAlignment="Left" Margin="4,2,4,2" Height="Auto" Width="Auto"
                                Visibility="{Binding Path=UnitVisibility, RelativeSource={RelativeSource TemplatedParent}}"
                               >
                            <TextBlock Text="{Binding Path=DisplayUnit, RelativeSource={RelativeSource TemplatedParent}}"/>
                        </Label>
                        <Label Target="{Binding ElementName=combobox1}" HorizontalAlignment="Left" HorizontalContentAlignment="Left" Margin="4,2,4,2" Height="Auto" Width="Auto" MaxWidth="450"
                                Visibility="{Binding Path=DescriptionVisibility, RelativeSource={RelativeSource TemplatedParent}}"
                               >
                            <AccessText Text="{Binding Path=Description, RelativeSource={RelativeSource TemplatedParent}}" ToolTip="{Binding Path=Description, RelativeSource={RelativeSource TemplatedParent}}" TextTrimming="WordEllipsis" TextWrapping="Wrap"/>
                        </Label>
                        </StackPanel>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黯淡〆 2024-11-02 01:39:21

您的图像的属性 - 您是否已创建它,以便在您访问该属性时而不是在构造函数中创建图像?听起来它是在构造函数中加载的:)

The property for your image - have you made it so it creates the image when you access the property and not in the constructor? It sounds like it is loaded in the constructor :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文