Nếu bạn không muốn nâng cấp lên .NET 4.5, bạn vẫn có thể đặt thuộc tính IsPixelBased
trên VirtualizingStackPanel cơ bản. Tuy nhiên tài sản này là nội bộ trong .NET 4.0, vì vậy bạn sẽ phải làm điều đó thông qua sự phản ánh.
public static class VirtualizingStackPanelBehaviors
{
public static bool GetIsPixelBasedScrolling(DependencyObject obj)
{
return (bool)obj.GetValue(IsPixelBasedScrollingProperty);
}
public static void SetIsPixelBasedScrolling(DependencyObject obj, bool value)
{
obj.SetValue(IsPixelBasedScrollingProperty, value);
}
// Using a DependencyProperty as the backing store for IsPixelBasedScrolling. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IsPixelBasedScrollingProperty =
DependencyProperty.RegisterAttached("IsPixelBasedScrolling", typeof(bool), typeof(VirtualizingStackPanelBehaviors), new UIPropertyMetadata(false, OnIsPixelBasedScrollingChanged));
private static void OnIsPixelBasedScrollingChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
var virtualizingStackPanel = o as VirtualizingStackPanel;
if (virtualizingStackPanel == null)
throw new InvalidOperationException();
var isPixelBasedPropertyInfo = typeof(VirtualizingStackPanel).GetProperty("IsPixelBased", BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.NonPublic);
if (isPixelBasedPropertyInfo == null)
throw new InvalidOperationException();
isPixelBasedPropertyInfo.SetValue(virtualizingStackPanel, (bool)(e.NewValue), null);
}
}
Và trong XAML của bạn:
<DataGrid>
<DataGrid.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel IsItemsHost="True" local:VirtualizingStackPanelBehaviors.IsPixelBasedScrolling="True" />
</ItemsPanelTemplate>
</DataGrid.ItemsPanel>
</DataGrid>
Không thể bạn quấn lưu ý trong một 'ScrollViewer' mà' MaxHeight' được thiết lập để chiều cao của DataGrid? – McGarnagle
@dbaseman Tôi muốn tiêu đề luôn hiển thị và điều đó sẽ vô hiệu hóa ảo hóa của DataGrid vì nó sẽ hiển thị tất cả các mục – Rachel
Bạn đã thử đặt 'CanContentScroll' thành' False' cho ScrollViewer của dataGrid chưa? –