10

メモでいっぱいの DataGrid がありますが、メモが DataGrid の高さよりも高くなる可能性があります。これが発生した場合、メモの下半分を読むために下にスクロールしようとすると、DataGrid はすぐに次の行にスキップします。

これにより、ユーザーがメモ全体を表示できなくなるだけでなく、スクロールが飛び跳ねるように見えるため、スクロールが不安定に感じられます。

デフォルトの DataGrid 仮想化を無効にせずに、長いメモをスムーズにスクロールするように WPF に指示する方法はありますか?

4

2 に答える 2

26

VirtualizingPanel.ScrollUnitDataGrid で設定できる添付プロパティを探していると思います。

その値をPixeldefault の代わりに に設定するとItem、希望どおりに動作するはずです。

于 2012-11-14T17:37:51.967 に答える
2

.NET 4.5 にアップグレードしたくない場合でもIsPixelBased、基になる VirtualizingStackPanel でプロパティを設定できます。ただし、このプロパティは .NET 4.0 では内部的なものであるため、リフレクションを介して行う必要があります。

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);
    }
}

そしてあなたの xaml で:

<DataGrid>
    <DataGrid.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel IsItemsHost="True" local:VirtualizingStackPanelBehaviors.IsPixelBasedScrolling="True" />
        </ItemsPanelTemplate>
    </DataGrid.ItemsPanel>
</DataGrid>
于 2012-11-14T17:53:29.507 に答える