我有一个使用VS 2008开发的WPF项目,它使用.NET框架的3.5版本。初始窗口宽度设置为680,SizeToContent仅设置为“宽度”。在此窗口中有3个ListView控件放置在网格面板中。前两个网格都有一列指定宽度。第3个ListView控件未指定任何列的宽度。当ListView控件最初填充数据时,窗口会正确调整大小。如果其他数据进入第3个ListView,则窗口会适当缩小或增大宽度。但是,如果第一个或第二个ListView容器增大或缩小 - 特别是如果通过垂直增加窗口大小来移除滚动条,则窗口的宽度未正确更新。有没有人知道如何在更新第2和第3个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>