我有一个使用 .NET 框架 3.5 版的 VS 2008 开发的 WPF 项目。初始窗口宽度设置为 680,SizeToContent 仅设置为“宽度”。此窗口的网格面板中放置了 3 个 ListView 控件。前 2 个网格都有一个指定宽度的列。第三个 ListView 控件没有指定任何列的宽度。当 ListView 控件最初填充数据时,窗口会正确调整大小。如果附加数据进入第三个 ListView,则窗口会相应地缩小或扩大宽度。但是,如果第一个或第二个 ListView 容器增长或缩小 - 特别是如果通过垂直增加窗口大小来移除滚动条,则窗口的宽度不会正确更新。
这是我的窗口和 ListView 控件的 XAML 代码:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:AP2"
x:Class="AP2.MainWindow"
x:Name="LobbyWindow"
Title="Lobby"
Height="480"
mc:Ignorable="d"
SizeToContent="Width"
MinWidth="680"
MinHeight="480"
SizeChanged="LobbyWindow_SizeChanged"
Loaded="LobbyWindow_Loaded"
Closing="LobbyWindow_Closing"
StateChanged="LobbyWindow_StateChanged">
<Window.Background>
<LinearGradientBrush EndPoint="0.5,1"
StartPoint="0.5,0">
<GradientStop Color="#FF434D7A"
Offset="0" />
<GradientStop Color="#FF180CFA"
Offset="1" />
</LinearGradientBrush>
</Window.Background>
...
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="0.064*" />
<RowDefinition Height="0.936*" />
</Grid.RowDefinitions>
<!-- Define the selected table label. -->
<Label x:Name="SelTableLabel"
Margin="1,-5,0,0"
Content="Table"
FontSize="11"
Foreground="#FF000000"
Background="#FFFFFFFF"
Grid.Row="0"
Grid.Column="2"
FontFamily="Georgia"
FontWeight="Bold"
VerticalAlignment="Bottom" />
<ListView x:Name="ListView1"
ItemContainerStyle="{StaticResource alternatingListViewItemStyle}"
AlternationCount="2"
SelectionChanged="ListView1_SelectionChanged"
Grid.Row="1"
Grid.Column="0"
SelectionMode="Multiple"
ItemsSource="{Binding ElementName=LobbyWindow, Path=ListCollection1}">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Game}">
<GridViewColumnHeader Content="Game"
FontWeight="Bold" />
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding Stakes}">
<GridViewColumnHeader Content="Stakes"
FontWeight="Bold" />
</GridViewColumn>
<GridViewColumn Width="30"
DisplayMemberBinding="{Binding Seats}">
<GridViewColumnHeader Content="Seats"
FontWeight="Bold" />
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
<ListView x:Name="ListView2"
ItemContainerStyle="{StaticResource alternatingListViewItemStyle}"
AlternationCount="2"
Grid.Row="1"
Grid.Column="1"
SelectionChanged="ListView2_SelectionChanged"
SelectionMode="Single"
ItemsSource="{Binding ElementName=LobbyWindow, Path=ListCollection2}">
<ListView.View>
<GridView>
<GridViewColumn Width="90"
x:Name="TableName"
DisplayMemberBinding="{Binding TableName}">
<GridViewColumnHeader Content="Table"
Width="90"
FontWeight="Bold"
HorizontalAlignment="Left" />
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding Stakes}">
<GridViewColumnHeader Content="Stakes"
FontWeight="Bold" />
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding NumPlayers}">
<GridViewColumnHeader Content="Players"
FontWeight="Bold" />
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding Speed}">
<GridViewColumnHeader Content="Speed"
FontWeight="Bold" />
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding HandsPerHour}">
<GridViewColumnHeader Content="H/Hr"
FontWeight="Bold" />
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding AvgPotSize}">
<GridViewColumnHeader Content="Avg Pot"
FontWeight="Bold" />
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
<ListView x:Name="ListView3"
ItemContainerStyle="{StaticResource alternatingListViewItemStyle}"
AlternationCount="2"
Grid.Row="1"
Grid.Column="2"
SelectionMode="Single"
ItemsSource="{Binding ElementName=LobbyWindow, Path=ListCollection3}">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Player}">
<GridViewColumnHeader Content="Player"
FontWeight="Bold" />
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding City}">
<GridViewColumnHeader Content="City"
FontWeight="Bold" />
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding Chips}">
<GridViewColumnHeader Content="Chips"
FontWeight="Bold" />
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>