I want to set initial size of listview
. ListView
is docked in DockPanel
as a last child with property LastChildFill = true. The Window has SizeToContent set to SizeToContent.Height.
I want Window to change size as user resizes it and I want ListView to change its size respectively.
But I'd like ListView height is 200 as Window opens.
How to achieve it?
If possible, calculate the window height needed when the listbox is 200. Then simply set the height of the window to that number like this:
<Window x:Class="WPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="200" Height="225"
Title="MainWindow" >
<DockPanel LastChildFill="True" VerticalAlignment="Stretch">
<TextBlock DockPanel.Dock="Top">Other text</TextBlock>
<ListView BorderBrush="Orange" VerticalAlignment="Stretch" />
</DockPanel>
</Window>
If this is not possible, (eg you don't know in advance the size of the other elements), then do the following:
<Window x:Class="WPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="200" SizeToContent="Height"
Title="MainWindow" Loaded="Window_Loaded" >
<DockPanel LastChildFill="True">
<TextBlock DockPanel.Dock="Top">Other Text</TextBlock>
<ListView BorderBrush="Orange" Name="listbox" Height="200" />
</DockPanel>
</Window>
and in code behind:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Height = ActualHeight;
SizeToContent = System.Windows.SizeToContent.Manual;
listbox.Height = Double.NaN;
}