我不喜欢在这里粘贴相当长的代码片段,但是我遇到了以下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>
答案 0 :(得分: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>