2015-07-23 5 views
0

Je souhaite définir la taille initiale de listview. ListView est ancré dans DockPanel en tant que dernier enfant avec la propriété LastChildFill = true. La fenêtre a SizeToContent défini sur SizeToContent.Height. Je veux que Window change de taille lorsque l'utilisateur le redimensionne et que ListView change respectivement de taille. Mais j'aimerais que la taille de ListView soit de 200 lorsque la fenêtre s'ouvre.Comment définir la taille initiale dans la fenêtre WPF ancrée dans DockPanel

Comment y parvenir?

Répondre

0

Si possible, calculer la hauteur de la fenêtre nécessaire lorsque la zone de liste est de 200. Ensuite, il suffit régler la hauteur de la fenêtre à ce numéro comme ceci:

<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> 

Si cela est impossible, (par exemple, vous n » t savoir à l'avance la taille des autres éléments), puis procédez comme suit:

<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> 

et code derrière:

private void Window_Loaded(object sender, RoutedEventArgs e) 
{ 
    Height = ActualHeight; 
    SizeToContent = System.Windows.SizeToContent.Manual; 
    listbox.Height = Double.NaN; 
} 
+0

Deuxième solution avec réglage mininal fonctionne pour moi, merci. – ijon

1

Ma solution

<Window x:Class="WPF.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Width="640" SizeToContent="Height" 
     Title="MainWindow" Loaded="Window_Loaded" > 
    <DockPanel LastChildFill="True"> 
     <TextBlock DockPanel.Dock="Top">Other Text</TextBlock> 
     <ListView Name="listbox" /> 
    </DockPanel> 
</Window> 

et code derrière:

private void Window_Loaded(object sender, RoutedEventArgs e) 
{ 
    SizeToContent = System.Windows.SizeToContent.Manual; 
    Height = 480; 
}