2016-02-12 78 views
0

一直在尋找這4小時,傢伙...我實在不明白這一點(可能是微軟的錯誤?)WPF的DataGrid超出電網

這是我,這一切都只是DataGrid控件好。正如你可以在這個視頻中看到,數據超出了應用程序的邊界(網格和滾動條):

https://goo.gl/photos/YnApkZS7v3uZ4TWX6

因爲這個代碼是使用材料設計庫,我剝離下來,以基礎知識,使任何人都可以試試爲自己。

<Window 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    x:Class="EvolvDirectoryCreeper.MainWindow" 
    Height="700" Width="1000"> 
<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto"/> 
     <RowDefinition Height="30"/> 
     <RowDefinition Height="30"/> 
     <RowDefinition Height="*"/> 
    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition/> 
     <ColumnDefinition Width="Auto" MinWidth="80"/> 
    </Grid.ColumnDefinitions> 
    <Grid Height="180" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" Grid.RowSpan="2"> 
     <Grid Margin="10,10,100,10"> 
      <Grid.RowDefinitions> 
       <RowDefinition/> 
       <RowDefinition/> 
       <RowDefinition/> 
      </Grid.RowDefinitions> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition/> 
       <ColumnDefinition/> 
      </Grid.ColumnDefinitions> 
      <TextBox Grid.Column="0" Grid.Row="0" Margin="4"/> 
      <TextBox Grid.Column="0" Grid.Row="1" Margin="4"/> 
      <TextBox Grid.Column="0" Grid.Row="2" Margin="4"/> 
      <Button Grid.Column="1" Grid.Row="0" Margin="4"/> 
      <Button Grid.Column="1" Grid.Row="1" Margin="4"/> 
      <Button Grid.Column="1" Grid.Row="2" Margin="4"/> 
     </Grid> 
    </Grid> 
    <Grid Grid.Column="0" Grid.Row="3" Grid.ColumnSpan="2" Margin="10"> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="400"/> 
      <ColumnDefinition Width="*"/> 
     </Grid.ColumnDefinitions> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="*"/> 
     </Grid.RowDefinitions> 
     <Label Height="40" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" Margin="4" Padding="0,15,0,0" HorizontalAlignment="Left" VerticalAlignment="Bottom"/> 
     <ProgressBar Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" Margin="4" VerticalAlignment="Bottom"/> 
     <Label Content="Good" Grid.Column="0" Grid.Row="1" Margin="4,30,0,0"/> 
     <Label Content="Bad" Grid.Column="2" Grid.Row="1" Margin="4,30,0,0"/> 
     <DataGrid Grid.Column="0" Grid.Row="2"/> 
     <DataGrid Grid.Column="2" Grid.Row="2" ItemsSource="{Binding Path=BadFoldersHistory}"/> 
    </Grid> 
</Grid> 

這是C#:

using System; 
using System.Collections.Generic; 
using System.Collections.ObjectModel; 
using System.ComponentModel; 
using System.Diagnostics; 
using System.Globalization; 
using System.IO; 
using System.Linq; 
using System.Reflection; 
using System.Text.RegularExpressions; 
using System.Threading; 
using System.Threading.Tasks; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Threading; 

namespace EvolvDirectoryCreeper 
{ 
    public partial class MainWindow : Window, INotifyPropertyChanged 
    { 
     private ObservableCollection<string> m_badFoldersHistory = new ObservableCollection<string>(); 
     public ObservableCollection<string> BadFoldersHistory 
     { 
      get { return m_badFoldersHistory; } 
      set { 
       m_badFoldersHistory = value;                   
       OnPropertyChanged("BadFoldersHistory"); 
      } 
     } 

     public virtual event PropertyChangedEventHandler PropertyChanged; 
     public virtual void OnPropertyChanged(string name) 
     { 
      PropertyChangedEventHandler handler = PropertyChanged; 
      if (handler != null) 
      { 
       handler(this, new PropertyChangedEventArgs(name)); 
      } 
     } 

     private Dispatcher MainWindowDispatcher; 

     public MainWindow() 
     { 
      InitializeComponent(); 

      DataContext = this; 
      MainWindowDispatcher = Dispatcher; 

      for (int i = 0; i < 40; i++) 
       BadFoldersHistory.Add("d"); 
     } 
    } 
} 

我已經嘗試過的,而不是 「*」, 「汽車」,每個網格的最後一排,但於事無補。我需要保持這個網格結構來維護材料設計FAB的位置並適當調整大小。 任何幫助將不勝感激!

回答

1

某處需要定義高度。我把第一個RowDef設爲180,因爲你的第一個網格是180.試試下面的代碼。

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="180"/> 
     <RowDefinition Height="30"/> 
     <RowDefinition Height="30"/> 
     <RowDefinition Height="*"/> 
    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition/> 
     <ColumnDefinition Width="Auto" MinWidth="80"/> 
    </Grid.ColumnDefinitions> 
    <Grid Height="180" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" Grid.RowSpan="2"> 
     <Grid Margin="10,10,100,10"> 
      <Grid.RowDefinitions> 
       <RowDefinition/> 
       <RowDefinition/> 
       <RowDefinition/> 
      </Grid.RowDefinitions> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition/> 
       <ColumnDefinition/> 
      </Grid.ColumnDefinitions> 
      <TextBox Grid.Column="0" Grid.Row="0" Margin="4"/> 
      <TextBox Grid.Column="0" Grid.Row="1" Margin="4"/> 
      <TextBox Grid.Column="0" Grid.Row="2" Margin="4"/> 
      <Button Grid.Column="1" Grid.Row="0" Margin="4"/> 
      <Button Grid.Column="1" Grid.Row="1" Margin="4"/> 
      <Button Grid.Column="1" Grid.Row="2" Margin="4"/> 
     </Grid> 
    </Grid> 
    <Grid Grid.Column="0" Grid.Row="3" Grid.ColumnSpan="2" Margin="10"> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="400"/> 
      <ColumnDefinition Width="*"/> 
     </Grid.ColumnDefinitions> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="20"/> 
      <RowDefinition Height="20"/> 
      <RowDefinition Height="*"/> 
     </Grid.RowDefinitions> 
     <Label Height="40" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" Margin="4" Padding="0,15,0,0" 
       HorizontalAlignment="Left" VerticalAlignment="Bottom"/> 
     <ProgressBar Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" Margin="4" VerticalAlignment="Bottom"/> 
     <Label Content="Good" Grid.Column="0" Grid.Row="1" Margin="4,30,0,0"/> 
     <Label Content="Bad" Grid.Column="2" Grid.Row="1" Margin="4,30,0,0"/> 
     <DataGrid Grid.Column="0" Grid.Row="2"/> 
     <DataGrid Grid.Column="2" Grid.Row="2" ItemsSource="{Binding Path=BadFoldersHistory}" /> 
    </Grid> 
</Grid> 
+0

沒錯!這就是它!謝謝! – SYB

+0

順便說一句,任何對Material Design WPG庫感興趣的人,我都強烈推薦:https://github.com/ButchersBoy/MaterialDesignInXamlToolkit – SYB

0

一個通用的解決方案將是把控制,可以顯示content如果超過父控件的高度。這是類似WPF中的控件(如Window,ContentControl或任何Panel等)的ContentControl的一個非常常見的問題。

See this link to see the generic problem

我們無法確定內部控制所有時代的高度,這樣的修復 高度的解決方案是不是最好的解決方案。如果控制高度超過 或匹配您的grid的高度,則固定高度解決方案爲kaput。

一個通用的解決方案是包含您grid在能夠處理可變高度的控制(如Scrollviewer)。如下圖所示:

<ScrollViewer MaxHeight="{Binding RelativeSource={RelativeSource AncestorType={x:Type Border}},Path=ActualHeight,Mode=OneWay}"> 
    <Grid> 
............ 
</ScrollViewer> 

而且你不必擔心任何控件的高度從grid/Window的高度被超過。

輸出:

ScrollViewer