2

我不喜欢在此处粘贴相当长的代码片段,但我对以下 WPF UserControl(本文末尾的 XAML)有疑问。提供的代码已简化,但我对其进行了测试,并且它是可重现的。

这个 UserControl的问题是,当添加到 WPF 窗口时,水平调整大小非常慢。更准确地说,当窗口的宽度增加时,调整大小的速度与预期一样快。但是,一旦它的宽度减小,包含的 UserControl 就会挂起并且只会非常缓慢地调整大小。在我的电脑上,大约需要 3 秒才能达到最终大小。

我现在的问题是: 为什么会这样,我该怎么办?有了这个简化的控件,可能问题不大,但是如果两个 ListView 都填满了,操作就更慢了。这是 WPF 本身的问题还是 XAML 中有错误?我不知道。任何提示/解决方案/解决方法/评论表示赞赏!:-) 谢谢!

<UserControl x:Class="TestApplication.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             d:DesignHeight="768" d:DesignWidth="1280" mc:Ignorable="d">
    <UserControl.Resources>
        <ResourceDictionary>
            <!-- [removed for clarity] -->
        </ResourceDictionary>
    </UserControl.Resources>

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="{Binding ElementName='uiCustomerOrderListView', Path='ActualWidth'}" />
            <ColumnDefinition Width="5" />
            <ColumnDefinition Width="{Binding ElementName='uiPackagingOrderListView', Path='ActualWidth'}" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <Label
            BorderBrush="Black"
            BorderThickness="1"
            Content="Customer Orders"
            FontWeight="Bold"
            Grid.Column="0"
            Grid.Row="0"
            HorizontalContentAlignment="Center"
            Margin="0,0,0,2"
            SnapsToDevicePixels="True"
            VerticalContentAlignment="Center" />
        <Label
            BorderBrush="Black"
            BorderThickness="1"
            Content="Packaging Orders"
            FontWeight="Bold"
            Grid.Column="2"
            Grid.Row="0"
            HorizontalContentAlignment="Center"
            Margin="0,0,0,2"
            SnapsToDevicePixels="True"
            VerticalContentAlignment="Center" />

        <Grid
            Grid.Column="0"
            Grid.Row="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="4" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="4" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>

            <TextBlock
                x:FieldModifier="private"
                x:Name="uiCustomerOrdersColumn00HeaderTextBlock"
                FontWeight="Bold"
                Grid.Column="1"
                Grid.Row="0"
                Text="[Order No.]"
                TextAlignment="Center"
                TextTrimming="CharacterEllipsis"
                ToolTip="" />
            <!-- [similar TextBlocks for columns 2, 3, 4, 5, 6] -->
            <!-- [removed for clarity] -->
        </Grid>

        <Grid
            Grid.Column="2"
            Grid.Row="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="4" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="4" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>

            <TextBlock
                x:FieldModifier="private"
                x:Name="uiPackagingOrdersColumn00HeaderTextBlock"
                FontWeight="Bold"
                Grid.Column="1"
                Grid.Row="0"
                Text="[Order Nr.]"
                TextAlignment="Center"
                TextTrimming="CharacterEllipsis"
                ToolTip="" />
            <!-- [similar TextBlocks for columns 2, 3, 4, 5, 6, 7] -->
            <!-- [removed for clarity] -->
        </Grid>

        <Border
            BorderBrush="Black"
            BorderThickness="0,2,0,0"
            Grid.Column="0"
            Grid.ColumnSpan="4"
            Grid.Row="2"
            Margin="1,1,1,1"
            SnapsToDevicePixels="True" />

        <ScrollViewer
            Grid.Column="0"
            Grid.ColumnSpan="4"
            Grid.Row="3"
            HorizontalScrollBarVisibility="Disabled"
            VerticalScrollBarVisibility="Visible">

            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="5" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>

                <ListView
                    x:FieldModifier="private"
                    x:Name="uiCustomerOrderListView"
                    Grid.Column="0"
                    Grid.Row="1"
                    ItemsSource="{Binding Path='.'}"
                    ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                    ScrollViewer.VerticalScrollBarVisibility="Disabled"
                    SelectionMode="Single"
                    VirtualizingStackPanel.IsVirtualizing="False">
                    <ListView.View>
                        <GridView
                            x:FieldModifier="private"
                            x:Name="uiCustomerOrderGridView"
                            AllowsColumnReorder="False">
                            <GridView.Columns>
                                <GridViewColumn />
                                <GridViewColumn />
                                <GridViewColumn />
                                <GridViewColumn />
                                <GridViewColumn />
                                <GridViewColumn />
                            </GridView.Columns>
                        </GridView>
                    </ListView.View>
                </ListView>

                <ListView
                    x:FieldModifier="private"
                    x:Name="uiPackagingOrderListView"
                    Grid.Column="2"
                    Grid.Row="1"
                    ItemsSource="{Binding Path='.'}"
                    ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                    ScrollViewer.VerticalScrollBarVisibility="Disabled"
                    SelectionMode="Single"
                    VirtualizingStackPanel.IsVirtualizing="False">
                    <ListView.View>
                        <GridView
                            x:FieldModifier="private"
                            x:Name="uiPackagingOrderGridView"
                            AllowsColumnReorder="False">
                            <GridView.Columns>
                                <GridViewColumn />
                                <GridViewColumn />
                                <GridViewColumn />
                                <GridViewColumn />
                                <GridViewColumn />
                                <GridViewColumn />
                                <GridViewColumn />
                            </GridView.Columns>
                        </GridView>
                    </ListView.View>
                </ListView>
            </Grid>
        </ScrollViewer>
    </Grid>
</UserControl>
4

1 回答 1

2

这是因为您将顶部网格(客户订单/包装订单)的宽度绑定到其他控件的实际宽度,因此在调整大小时它会经历大量更新。为了在我的测试工具中修复它并保持相同的外观,我执行了以下操作:

将第一个网格的列设置为:

<Grid.ColumnDefinitions>
     <ColumnDefinition Width="*" />            
     <ColumnDefinition Width="*" />
     <ColumnDefinition Width="18"/>
</Grid.ColumnDefinitions>

然后,我修改了您的第二个标签,将Grid.Column属性设置为 1 而不是 2。这为您提供了相同的整体外观。我会说虽然你使用了很多网格并且可能会简化这个设计。这是我的完整代码版本:

<Grid>
     <Grid.ColumnDefinitions>
     <ColumnDefinition Width="*" />            
     <ColumnDefinition Width="*" />
     <ColumnDefinition Width="18"/>
  </Grid.ColumnDefinitions>
     <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
     </Grid.RowDefinitions>

     <Label
        BorderBrush="Black"
        BorderThickness="1"
        Content="Customer Orders"
        FontWeight="Bold"
        Grid.Column="0"
        Grid.Row="0"
        HorizontalContentAlignment="Center"
        Margin="0,0,0,2"
        SnapsToDevicePixels="True"
        VerticalContentAlignment="Center" />
     <Label
        BorderBrush="Black"
        BorderThickness="1"
        Content="Packaging Orders"
        FontWeight="Bold"
        Grid.Column="1"
        Grid.Row="0"
        HorizontalContentAlignment="Center"
        Margin="0,0,0,2"
        SnapsToDevicePixels="True"
        VerticalContentAlignment="Center" />

     <Grid
        Grid.Column="0"
        Grid.Row="1">
        <Grid.ColumnDefinitions>
           <ColumnDefinition Width="4" />
           <ColumnDefinition Width="*" />
           <ColumnDefinition Width="*" />
           <ColumnDefinition Width="*" />
           <ColumnDefinition Width="*" />
           <ColumnDefinition Width="*" />
           <ColumnDefinition Width="*" />
           <ColumnDefinition Width="4" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
           <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <TextBlock
            x:FieldModifier="private"
            x:Name="uiCustomerOrdersColumn00HeaderTextBlock"
            FontWeight="Bold"
            Grid.Column="1"
            Grid.Row="0"
            Text="[Order No.]"
            TextAlignment="Center"
            TextTrimming="CharacterEllipsis"
            ToolTip="" />
        <!-- [similar TextBlocks for columns 2, 3, 4, 5, 6] -->
        <!-- [removed for clarity] -->
     </Grid>

     <Grid
        Grid.Column="2"
        Grid.Row="1">
        <Grid.ColumnDefinitions>
           <ColumnDefinition Width="4" />
           <ColumnDefinition Width="*" />
           <ColumnDefinition Width="*" />
           <ColumnDefinition Width="*" />
           <ColumnDefinition Width="*" />
           <ColumnDefinition Width="*" />
           <ColumnDefinition Width="*" />
           <ColumnDefinition Width="*" />
           <ColumnDefinition Width="4" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
           <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <TextBlock
            x:FieldModifier="private"
            x:Name="uiPackagingOrdersColumn00HeaderTextBlock"
            FontWeight="Bold"
            Grid.Column="1"
            Grid.Row="0"
            Text="[Order Nr.]"
            TextAlignment="Center"
            TextTrimming="CharacterEllipsis"
            ToolTip="" />
        <!-- [similar TextBlocks for columns 2, 3, 4, 5, 6, 7] -->
        <!-- [removed for clarity] -->
     </Grid>

     <Border
        BorderBrush="Black"
        BorderThickness="0,2,0,0"
        Grid.Column="0"
        Grid.ColumnSpan="4"
        Grid.Row="2"
        Margin="1,1,1,1"
        SnapsToDevicePixels="True" />

     <ScrollViewer
        Grid.Column="0"
        Grid.ColumnSpan="4"
        Grid.Row="3"
        HorizontalScrollBarVisibility="Disabled"
        VerticalScrollBarVisibility="Visible">

        <Grid>
           <Grid.ColumnDefinitions>
              <ColumnDefinition Width="*" />
              <ColumnDefinition Width="5" />
              <ColumnDefinition Width="*" />
           </Grid.ColumnDefinitions>
           <Grid.RowDefinitions>
              <RowDefinition Height="*" />
           </Grid.RowDefinitions>

           <ListView
                x:FieldModifier="private"
                x:Name="uiCustomerOrderListView"
                Grid.Column="0"
                Grid.Row="1"
                ItemsSource="{Binding Path='.'}"
                ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                ScrollViewer.VerticalScrollBarVisibility="Disabled"
                SelectionMode="Single"
                VirtualizingStackPanel.IsVirtualizing="False">
              <ListView.View>
                 <GridView
                        x:FieldModifier="private"
                        x:Name="uiCustomerOrderGridView"
                        AllowsColumnReorder="False">
                    <GridView.Columns>
                       <GridViewColumn />
                       <GridViewColumn />
                       <GridViewColumn />
                       <GridViewColumn />
                       <GridViewColumn />
                       <GridViewColumn />
                    </GridView.Columns>
                 </GridView>
              </ListView.View>
           </ListView>

           <ListView
                x:FieldModifier="private"
                x:Name="uiPackagingOrderListView"
                Grid.Column="2"
                Grid.Row="1"
                ItemsSource="{Binding Path='.'}"
                ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                ScrollViewer.VerticalScrollBarVisibility="Disabled"
                SelectionMode="Single"
                VirtualizingStackPanel.IsVirtualizing="False">
              <ListView.View>
                 <GridView
                        x:FieldModifier="private"
                        x:Name="uiPackagingOrderGridView"
                        AllowsColumnReorder="False">
                    <GridView.Columns>
                       <GridViewColumn />
                       <GridViewColumn />
                       <GridViewColumn />
                       <GridViewColumn />
                       <GridViewColumn />
                       <GridViewColumn />
                       <GridViewColumn />
                    </GridView.Columns>
                 </GridView>
              </ListView.View>
           </ListView>
        </Grid>
     </ScrollViewer>
  </Grid>
于 2012-08-07T15:35:08.010 回答