2014-05-17 32 views
1

的,我有以下XAML代碼:按鈕是不可見的,因爲數據網格

<Window x:Class="DataGridIssue.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:vm="clr-namespace:DataGridIssue" 
     Title="MainWindow" Height="350" Width="525"> 
    <Window.Resources> 
     <vm:ViewModel x:Key="ViewModel"/> 
    </Window.Resources> 
    <Grid> 
     <DockPanel VerticalAlignment="Top"> 
      <Button DockPanel.Dock="Top" Content="Test button 1" HorizontalAlignment="Left"/> 
      <DataGrid DockPanel.Dock="Top" AutoGenerateColumns="True" ItemsSource="{Binding Source={StaticResource ViewModel}, Path=TestList}"/> 
      <Button DockPanel.Dock="Top" HorizontalAlignment="Left" Content="Test button 2"/> 
     </DockPanel> 
    </Grid> 
</Window> 

和C#代碼視圖模型:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Collections.ObjectModel; 

namespace DataGridIssue 
{ 
    public class ViewModel 
    { 
     public ObservableCollection<Test> TestList { get; set; } 

     public ViewModel() 
     { 
      TestList = new ObservableCollection<Test>(); 

      for (int i = 1; i <= 30; i++) 
      { 
       var item = new Test(); 
       item.TestString = "Element " + i.ToString(); 
       TestList.Add(item); 
      } 
     } 
    } 

    public class Test 
    { 
     public string TestString { get; set; } 
    } 
} 

的事情是,第二個按鈕是不可見的。據我所知,按鈕總是在數據網格的末端(窗口外面會發生什麼),如果數據網格沒有垂直滾動,則該按鈕是可見的。這怎麼解決?

編輯:我想避免明確分配高度。

EDIT2:下面的伎倆:

<Window.Resources> 
     <vm:ViewModel x:Key="ViewModel"/> 
    </Window.Resources> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="*" /> 
      <RowDefinition Height="Auto" /> 
     </Grid.RowDefinitions> 

     <Button Grid.Row="0" Content="Test button 1" HorizontalAlignment="Left"/> 
     <DataGrid Grid.Row="1" AutoGenerateColumns="True" ItemsSource="{Binding Source={StaticResource ViewModel}, Path=TestList}"/> 
     <Button Grid.Row="2" HorizontalAlignment="Left" Content="Test button 2"/> 
    </Grid> 
</Window> 

回答

1

你應該使用Grid三行:

... 
<Window.Resources> 
     <vm:ViewModel x:Key="ViewModel"/> 
    </Window.Resources> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="*" /> 
      <RowDefinition Height="Auto" /> 
     </Grid.RowDefinitions> 

     <Button Grid.Row="0" Content="Test button 1" HorizontalAlignment="Left"/> 
     <DataGrid Grid.Row="1" AutoGenerateColumns="True" ItemsSource="{Binding Source={StaticResource ViewModel}, Path=TestList}"/> 
     <Button Grid.Row="2" HorizontalAlignment="Left" Content="Test button 2"/> 
    </Grid> 
</Window> 
+0

我想避免使用高度。 –

+0

@DmitryTimofeev你可以使用'Auto'自動調整行高。再次檢查我的答案:) – kmatyaszek

+0

是的,這有幫助!謝謝! –