У меня есть следующий код 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>
И код С# ViewModel:
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; }
}
}
Дело в том, что вторая кнопка не видна. Насколько я понимаю, кнопка всегда находится под концом datagrid (что происходит за окном), и если у datagrid нет вертикальной прокрутки, кнопка видна. Как это можно решить?
EDIT: Я бы хотел, чтобы я не задавал высоту.
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>
Вы должны использовать 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>
Auto
дляAuto
высоты строки. Проверьте мой ответ еще раз :)