我把一個ListView放在視圖的中間一行。該視圖包含在SizeToContent設置爲WidthAndHeight的窗口中。 ListView最初是空的,但底層的ViewModel在這個過程中填充了這個列表視圖。如何防止ListView擴大窗口尺寸?
中間Grid.Row高度設置爲*以填充窗口的可用大小。當ListView接收到新項目時,它會在某個時候擴大窗口大小,而不是在ListView中顯示ScrollViewer。我怎樣才能防止這種行爲有SizeToContent設置爲WidthAndHeight,Grid.Row高度*,但沒有ListView展開窗口尺寸?
下面是窗口(工作空間屬性將包含視圖模型)的代碼:
<Window x:Class="Views.ContainerWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="{Binding Title}"
SizeToContent="WidthAndHeight">
<ContentControl Content="{Binding Workspace}"/>
</Window>
爲提供視圖模型的視圖看起來是這樣的:
<UserControl x:Class="Views.SomeView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
MinHeight="450">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0"
TextWrapping="Wrap"
Margin="5"
Text="Some description text"/>
<ListView Grid.Row="1"
ItemsSource="{Binding ItemsList}"
Margin="5">
<ListView.View>
<GridView>
...
</GridView>
</ListView.View>
</ListView>
<Button Grid.Row="2"
HorizontalAlignment="Right"
Command" Value="{Binding CloseCommand}"/>
</Grid>
</UserControl>
謝謝。這實際上是我使用的解決方案。你可以看到,如果你擴展上面的評論。 :) – 2010-10-08 13:34:15
@Ninfendo:+1這是邪惡的,謝謝。我浪費了一個下午試圖弄清楚。 – TarkaDaal 2013-02-07 18:13:53