2012-05-11 74 views
2

我的DataGrid出現數據超出可視範圍。我已經研究了StackOverflow上的幾個類似的問題,但他們都沒有解決我的問題。我無法弄清楚我做錯了什麼。這就是我的xaml的樣子:水平滾動條沒有出現在DataGrid中

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="50"/> 
     <RowDefinition Height="*"/> 
    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="*"/> 
    </Grid.ColumnDefinitions>   
    <TextBlock HorizontalAlignment="Left" Foreground="{DynamicResource TitleFontColor}" 
    TextWrapping="Wrap" Text="{Binding WindowTitle}" VerticalAlignment="Center" 
    Margin="10,10,0,10" FontSize="18.667" FontFamily="Segoe UI" /> 
    <Border Background="{DynamicResource pnlworkarea}" 
     Grid.Row="1" CornerRadius="2" BorderThickness="1"> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="65"/> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="*"/> 
     </Grid.RowDefinitions> 
     <Border BorderThickness="1" 
     Background="{DynamicResource Brush_HeaderNew}" 
     BorderBrush="{DynamicResource pnlworkareastroke}" 
     CornerRadius="2" Margin="5"> 
     <Grid Margin="0"> 
       <ListBox ItemsSource="{Binding ViewMenuItems}" SelectedItem="{Binding Viewselected}" 
       VerticalAlignment="Center" Style="{DynamicResource Content_Header_ListBoxStyle}" 
     ItemTemplate="{DynamicResource Header_DataTemplate}" 
     ItemContainerStyle="{DynamicResource ContentHeader_ListBoxItemStyle}"    
     ItemsPanel="{DynamicResource Header_ItemsPanelTemplate}" HorizontalAlignment="Left"/>         
    </Grid> 
    </Border> 
    <Grid Grid.Row="1" Margin="5"> 
      <StackPanel HorizontalAlignment="Left" Orientation="Horizontal" VerticalAlignment="Top" Margin="10,0,0,0"> 
        <ContentControl Focusable="False" Content="ContentControl" 
     Background="{DynamicResource Brush_VectorIcon}" 
     Template="{DynamicResource vector_Summary}" 
     Height="16" Margin="0,5,0,0"/> 
     <TextBlock TextWrapping="Wrap" Text="{Binding SearchDisplayMessage}" Margin="5,3,0,0" VerticalAlignment="Center"/> 
      </StackPanel> 
     </Grid> 
     <Grid Grid.Row="2" Margin="5"> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="Auto"/> 
       <RowDefinition Height="*"/> 
      </Grid.RowDefinitions> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="*"/> 
       </Grid.ColumnDefinitions> 
       <DataGrid x:Name="dataGrid" 
        commands:ControlEvents.GridViewSelectionChangedEventCommand="{Binding SelectionItemsChangeCommand}" 
        SelectionMode="Single" 
        AutoGenerateColumns="True"      
        Margin="0,5" Grid.Row="1" IsReadOnly="True" 
        ItemsSource="{Binding GridItem, Mode=TwoWay}" SelectedItem="{Binding SelectedItem}" 
        AlternatingRowBackground="#FFF5F4F8" Background="White" Grid.ColumnSpan="2" 
        ScrollViewer.CanContentScroll="True" 
        ScrollViewer.HorizontalScrollBarVisibility="Auto" 
        ScrollViewer.VerticalScrollBarVisibility="Auto"> 
        <i:Interaction.Behaviors> 
         <Behaviors:SelectorDoubleClickCommandBehavior Command="{Binding GridDoubleclickcommand}"/> 
        </i:Interaction.Behaviors> 

       </DataGrid> 
      </Grid> 

     </Grid> 

    </Border> 

</Grid> 

只是爲了補充一點,這裏的DataGrid不是我正在使用的實際DataGrid。我的自定義DataGrid是從DataGrid派生的。但我已經試過這與正常的DataGrid,但似乎也沒有工作。

回答

1

你有自動

<Grid Grid.Row="2" Margin="5"> 
       <Grid.RowDefinitions> 
        <RowDefinition Height="Auto"/> 
        <RowDefinition Height="*"/> 
       </Grid.RowDefinitions> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="*"/> 
       </Grid.ColumnDefinitions> 
       <DataGrid x:Name="dataGrid" 
        SelectionMode="Single" 
        AutoGenerateColumns="True"      
        Margin="0,5" Grid.Row="1" IsReadOnly="True" 
        ItemsSource="{Binding GridItem, Mode=TwoWay}" SelectedItem="{Binding SelectedItem}" 
        AlternatingRowBackground="#FFF5F4F8" Background="White" Grid.ColumnSpan="2" 
        ScrollViewer.CanContentScroll="True" 
        ScrollViewer.HorizontalScrollBarVisibility="Auto" 
        ScrollViewer.VerticalScrollBarVisibility="Auto"> 
       </DataGrid> 
      </Grid> 

更改爲

高度
<DataGrid Grid.Row="2" x:Name="dataGrid" 
        SelectionMode="Single" 
        AutoGenerateColumns="True"      
        Margin="0,5" Grid.Row="1" IsReadOnly="True" 
        ItemsSource="{Binding GridItem, Mode=TwoWay}" SelectedItem="{Binding SelectedItem}" 
        AlternatingRowBackground="#FFF5F4F8" Background="White" Grid.ColumnSpan="2" 
        ScrollViewer.CanContentScroll="True" 
        ScrollViewer.HorizontalScrollBarVisibility="Auto" 
        ScrollViewer.VerticalScrollBarVisibility="Auto"> 
       </DataGrid> 
+0

我按照建議做了,但滾動條似乎並沒有出現,即使我將它設置爲可見。我甚至試圖將DataGrid放入一個ScrollViewer中,但在這種情況下,DataGrid延伸超出想象。我不介意在ScrollViewer中使用DataGrid,如果有一種方法可以限制DataGrid的列寬,從而允許每列伸展到它所包含數據的最大值。 –

+0

「The」滾動條 - 哪個?我沒有關注。你能以小代碼樣本重現問題嗎? – Paparazzi

+0

我已經設置了ScrollViewer.Horizo​​ntalScrollBarVisibility =「可見」。但是這並沒有幫助。我的時間真的很短。所以,作爲一種解決方法,我已經將DataGrid放在ScrollViewer中,並且在DataGrid的加載事件中,我將根據每個列中的內容大小調整列的大小。現在,問題似乎是我需要爲每列設置MinWidth,否則如果數據很少,它們會縮小太多。 –

1

我不能猜你在做什麼錯了,因爲你的代碼的最縮放dopwn版本顯示水平滾動條,我...

<UserControl.Resources> 
    <x:ArrayExtension x:Key="MyArray" Type="{x:Type TextBlock}"> 
     <TextBlock Name="Test1" Text="12345678" 
        DataContext="12345678" Tag="12345689"/> 
     <TextBlock Name="Test2" Text="12345678" 
        DataContext="12345678" Tag="12345689"/> 
     <TextBlock Name="Test1" Text="12345678" 
        DataContext="12345678" Tag="12345689"/> 
     <TextBlock Name="Test2" Text="12345678" 
        DataContext="12345678" Tag="12345689"/> 
     <TextBlock Name="Test1" Text="12345678" 
        DataContext="12345678" Tag="12345689"/> 
     <TextBlock Name="Test2" Text="12345678" 
        DataContext="12345678" Tag="12345689"/> 
     <TextBlock Name="Test1" Text="12345678" 
        DataContext="12345678" Tag="12345689"/> 
     <TextBlock Name="Test2" Text="12345678" 
        DataContext="12345678" Tag="12345689"/> 
     <TextBlock Name="Test1" Text="12345678" 
        DataContext="12345678" Tag="12345689"/> 
     <TextBlock Name="Test2" Text="12345678" 
        DataContext="12345678" Tag="12345689"/> 
     <TextBlock Name="Test1" Text="12345678" 
        DataContext="12345678" Tag="12345689"/> 
     <TextBlock Name="Test2" Text="12345678" 
        DataContext="12345678" Tag="12345689"/>    
    </x:ArrayExtension> 
</UserControl.Resources> 
<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="50"/> 
     <RowDefinition Height="*"/> 
    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="*"/> 
    </Grid.ColumnDefinitions> 
    <TextBlock HorizontalAlignment="Left" 
       TextWrapping="Wrap" 
       Text="My Text Clock" 
       VerticalAlignment="Center" 
       Margin="10,10,0,10" FontSize="18.667" 
       FontFamily="Segoe UI" /> 
    <Border Grid.Row="1" CornerRadius="2" BorderThickness="1"> 
     <Grid> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="65"/> 
       <RowDefinition Height="Auto" /> 
       <RowDefinition Height="*"/> 
      </Grid.RowDefinitions> 
      <Border BorderThickness="1" 
       CornerRadius="2" Margin="5"> 
       <Grid Margin="0"> 
         <ListBox 
          ItemsSource="{Binding 
           ViewMenuItems}"        
          VerticalAlignment="Center" 
          HorizontalAlignment="Left"/>    
    </Grid> 
    </Border> 
    <Grid Grid.Row="1" Margin="5"> 
      <StackPanel HorizontalAlignment="Left" 
         Orientation="Horizontal" 
         VerticalAlignment="Top" Margin="10,0,0,0"> 
        <ContentControl Focusable="False" 
            Content="ContentControl" 
            Height="16" Margin="0,5,0,0"/> 
        <TextBlock TextWrapping="Wrap" 
           Text="DisplayHerePlz!" 
           Margin="5,3,0,0" 
           VerticalAlignment="Center"/> 
       </StackPanel> 
    </Grid> 
     <Grid Grid.Row="2" Margin="5"> 
      <Grid.RowDefinitions> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="*"/> 
      </Grid.RowDefinitions> 
      <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="*"/> 
      </Grid.ColumnDefinitions> 
    <tk:DataGrid x:Name="dataGrid" 
      SelectionMode="Single" 
     AutoGenerateColumns="True" 
       Margin="0,5" Grid.Row="1" 
      IsReadOnly="True" 
      ItemsSource="{StaticResource MyArray}" 
      ScrollViewer.CanContentScroll="True" 
      ScrollViewer.HorizontalScrollBarVisibility="Auto" 
      ScrollViewer.VerticalScrollBarVisibility="Auto" 
     AlternatingRowBackground="#FFF5F4F8" 
      Background="White" 
      Grid.ColumnSpan="2">       
     </tk:DataGrid> 
    </Grid> 
    </Grid> 
    </Border> 
</Grid> 

它可能是我省略了其他位可能是導致水平寬度伸展超出滾動視圖...

+0

感謝您付出的努力。只是想知道是否有什麼明顯的我失蹤。我無法弄清楚嘗試不同的解決方案,看看是什麼問題。 –

0

這裏有幾件事你應該記住分配寬度屬性od datagrid視圖。定義您的列將最後一列的寬度設置爲auto,並指定一些minWidth。

  <DataGrid VerticalAlignment="Top" Margin="0,0,0,0" Height="221" HorizontalAlignment="Left" Width="234" 
       DataContext="{DynamicResource ItemCollectionViewSource}" 
         ItemsSource="{Binding}" AutoGenerateColumns="False" IsReadOnly="True" 
         ScrollViewer.CanContentScroll="True" 
         ScrollViewer.VerticalScrollBarVisibility="Auto" 
         ScrollViewer.HorizontalScrollBarVisibility="Auto" > 
       <DataGrid.Columns> 
        <DataGridTextColumn Binding="{Binding ID}" Header="ID"/> 
        <DataGridTextColumn Binding="{Binding URL}" Header="URL" Width="Auto" MinWidth="186"/> 
       </DataGrid.Columns> 
      </DataGrid> 
4

我有一個類似的問題,沒有顯示水平滾動條。我有一個寬度爲「*」的列。一旦我刪除了這個滾動條出現了。我想如果你將寬度設置爲固定的,它也會出現。

+0

這爲我工作(但我實際上已將其從*更改爲「自動」)。我相信滾動條會自動顯示一個字段是否在網格*上運行*,但是如果列的大小與網格的大小相同,則技術上它不會在網格上運行/觸發滾動條顯示。 – iliketocode