2014-11-04 34 views
20

在.NET 3.5中,我在窗口中有一個Grid。我用按鈕填充這個網格。當按鈕填充網格並離開視圖時,網格不顯示滾動條。我已將網格垂直滾動設置爲可見,但仍未顯示。WPF Grid不顯示滾動條

<Window x:Name="Window" x:Class="MergeToCheck.CheckList" 
      xmlns:sys="clr-namespace:System;assembly=mscorlib" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      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" 
      mc:Ignorable="d" Loaded="Window_Loaded" ScrollViewer.VerticalScrollBarVisibility="Disabled" 
       ResizeMode="NoResize" ShowInTaskbar="False" Topmost="True" WindowStyle="None" 
     Height="671" Width="846.299" BorderThickness="5"> 

    <Grid> 
     <Grid x:Name="MyGrid" HorizontalAlignment="Left" Height="535" VerticalAlignment="Top" Width="736" Margin="10,63,0,0" ScrollViewer.CanContentScroll="True" ScrollViewer.HorizontalScrollBarVisibility="Visible"> 
      <Grid.Resources> 
       <Style TargetType="{x:Type Panel}"> 
        <Setter Property="Margin" Value="0,0,0,6" /> 
       </Style> 
      </Grid.Resources> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition/> 
       <ColumnDefinition/> 
       <ColumnDefinition/> 
       <ColumnDefinition/> 
       <ColumnDefinition/> 
      </Grid.ColumnDefinitions> 
     </Grid>   
    </Grid> 
</Window> 

它增加了按鈕的代碼:

 CheckList CheckListCtrl = new CheckList(); 

     System.Windows.Controls.Button btn; 
     int row = 0; 
     int col = 0; 

     CheckListCtrl.MyGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(100) }); 

     foreach(var c in list) 
     { 
      btn = new System.Windows.Controls.Button(); 
      btn.FontSize = 15; 
      btn.FontWeight = FontWeights.UltraBold; 
      btn.Content = c.Name; 
      btn.Style = System.Windows.Application.Current.FindResource(System.Windows.Controls.ToolBar.ButtonStyleKey) as Style; 
      btn.BorderBrush = new SolidColorBrush(Colors.Black); 
      btn.BorderThickness = new Thickness(2); 
      btn.MinWidth = 145; 
      btn.MaxWidth = 145; 
      btn.MinHeight = 95; 
      btn.MaxHeight = 95; 

      btn.SetValue(Grid.RowProperty, row); 
      btn.SetValue(Grid.ColumnProperty, col); 

      CheckListCtrl.MyGrid.Children.Add(btn); 

      if ((col + 1) % CheckListCtrl.MyGrid.ColumnDefinitions.Count == 0) 
      {      
       col = 0; 
       row++; 
       CheckListCtrl.MyGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(100) }); 
      } 
      else 
       col++; 
     } 
+0

如果您的網格有足夠的空間來顯示所有項目,滾動條將不會出現。滾動條僅在物品多於空間時出現。你確定網格中有更多的項目比空間嗎? – Sonhja 2014-11-04 14:12:50

+0

是的,即時100%肯定它已經空間不足,但如果我設置ScrollViewer.Horizo​​ntalScrollBarVisibility =「可見」,它應該始終顯示滾動條,無論它是否有足夠的空間。 – CathalMF 2014-11-04 14:15:28

+2

網格不包含任何類型的滾動條。如果你想滾動,你需要'ScrollViewer'像' ...' – dkozl 2014-11-04 14:31:38

回答

49

Grid不支持滾動功能。如果你要滾動的東西,你需要ScrollViewer控制

<ScrollViewer HorizontalScrollBarVisibility="Visible"> 
    <Grid x:Name="MyGrid" HorizontalAlignment="Left" Height="535" VerticalAlignment="Top" Width="736" Margin="10,63,0,0"> 
     <Grid.Resources> 
     <Style TargetType="{x:Type Panel}"> 
      <Setter Property="Margin" Value="0,0,0,6" /> 
     </Style> 
     </Grid.Resources> 
     <Grid.ColumnDefinitions> 
     <ColumnDefinition/> 
     <ColumnDefinition/> 
     <ColumnDefinition/> 
     <ColumnDefinition/> 
     <ColumnDefinition/> 
     </Grid.ColumnDefinitions> 
    </Grid>   
</ScrollViewer> 
+0

太好了。有用。 – Zeeshan 2015-12-12 06:10:08