我有一个DataGrid绑定到主窗口内的一些数据。添加足够的行时,垂直滚动条会按原样显示。但是,它太高,以至于底部滚动按钮不可见,最后一行被切掉。
这是一个简化示例的图片,滚动条一直向下:
有谁知道发生了什么事?我猜它与WrapPanel
有关,不能按照我预期的方式自动调整它的高度。
以下是代码:
MainWindow.xaml
<Window x:Class="TestScroll.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="200" Width="450">
<WrapPanel>
<TextBlock
Text="TextBlock"
Width="400"
Height="40"/>
<DataGrid
x:Name="Control"
AutoGenerateColumns="False"
CanUserAddRows="False"
HeadersVisibility="Column"
Width="400"
ItemsSource="{Binding}" >
<DataGrid.Columns>
<DataGridTextColumn
Binding="{Binding Factor}"
Header="Header">
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</WrapPanel>
</Window>
MainWindow.xaml.cs
using System.Collections.ObjectModel;
using System.Windows;
namespace TestScroll
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var Table = new ObservableCollection<ViewModel>();
this.DataContext = Table;
for (int i = 0; i < 10; ++i)
{
var row = new ViewModel();
row.Symbol = i.ToString();
row.Factor = decimal.Parse(row.Symbol) / 100;
Table.Add(row);
}
}
}
}
ViewModel.cs
using System.ComponentModel;
namespace TestScroll
{
public class ViewModel : INotifyPropertyChanged
{
private string _symbol;
public string Symbol
{
get { return _symbol; }
set
{
_symbol = value;
OnPropertyChanged("Symbol");
}
}
private decimal _factor;
public decimal Factor
{
get { return _factor; }
set
{
_factor = value;
OnPropertyChanged("Factor");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
var e = new PropertyChangedEventArgs(propertyName);
if (handler != null)
handler(this, e);
}
}
}
答案 0 :(得分:1)
试试这个,
<DockPanel>
<TextBlock DockPanel.Dock="Top"
.../>
<DataGrid...>
</DataGrid>
</DockPanel>
修改正在用DockPanel替换WrapPanel并设置第一个Child的DockPanel.Dock =“Top”。