2016-05-20 9 views
-1

なぜこのWPFウィンドウは、この場合、グリッドまたはリストボックスのサイズ(したがってリストボックス内の項目の数に依存)?:wpfウィンドウをListBoxコンテンツに自動フィットする方法は?

<controls:MetroWindow x:Class="....." 
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
         xmlns:controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         Width="Auto" Height="Auto" MaxHeight="500" MaxWidth="500"> 

<Grid Background="Gray" Width="Auto" Height="Auto" > 
    <ListBox Width="Auto" Height="Auto" FontSize="12" FontWeight="SemiBold" ItemsSource="{Binding ListError}" > 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <TextBlock Text="{Binding}" /> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 
</Grid> 
</controls:MetroWindow> 

に依存し、その幅と高さを自動的に設定しないん、それを500 * 500(=私のMaxWidthとMaxHeightの値)をレンダリングします。 MaxWidthとMaxHeightの値を削除すると、リストボックスのサイズに自動調整されません。

Thxです。

答えて

3

HeightWidthプロパティを削除して、次の行を追加します。

SizeToContent="WidthAndHeight" 
+0

ThxをトンXiaoy312 :) –

関連する問題