5

ツリービューをラップするスクロールビューアがあります。

プログラムでツリービューにデータを入力し (バインドされていません)、ツリービューを所定のツリービュー項目に展開します。それはすべてうまくいきます。

私の問題は、ツリーが展開されたときに、ツリービューの親であるスクロールビューを、展開したばかりのツリービュー項目までスクロールすることです。何か案は?-ツリービューは展開されるたびに同じ構造を持たない可能性があることに注意してください。そのため、現在のスクロール位置を保存してそれにリセットすることはできません...

4

2 に答える 2

4

TreeView が選択した項目までスクロールしないという同じ問題がありました。

ツリーを選択した TreeViewItem に展開した後、Dispatcher Helper メソッドを呼び出して UI を更新できるようにし、選択した項目で TransformToAncestor を使用して、ScrollViewer 内の位置を見つけました。コードは次のとおりです。

    // Allow UI Rendering to Refresh
    DispatcherHelper.WaitForPriority();

    // Scroll to selected Item
    TreeViewItem tvi = myTreeView.SelectedItem as TreeViewItem;
    Point offset = tvi.TransformToAncestor(myScroll).Transform(new Point(0, 0));
    myScroll.ScrollToVerticalOffset(offset.Y);

DispatcherHelper コードは次のとおりです。

public class DispatcherHelper
{
    private static readonly DispatcherOperationCallback exitFrameCallback = ExitFrame;

    /// <summary>
    /// Processes all UI messages currently in the message queue.
    /// </summary>
    public static void WaitForPriority()
    {
        // Create new nested message pump.
        DispatcherFrame nestedFrame = new DispatcherFrame();

        // Dispatch a callback to the current message queue, when getting called,
        // this callback will end the nested message loop.
        // The priority of this callback should be lower than that of event message you want to process.
        DispatcherOperation exitOperation = Dispatcher.CurrentDispatcher.BeginInvoke(
            DispatcherPriority.ApplicationIdle, exitFrameCallback, nestedFrame);

        // pump the nested message loop, the nested message loop will immediately
        // process the messages left inside the message queue.
        Dispatcher.PushFrame(nestedFrame);

        // If the "exitFrame" callback is not finished, abort it.
        if (exitOperation.Status != DispatcherOperationStatus.Completed)
        {
            exitOperation.Abort();
        }
    }

    private static Object ExitFrame(Object state)
    {
        DispatcherFrame frame = state as DispatcherFrame;

        // Exit the nested message loop.
        frame.Continue = false;
        return null;
    }
}
于 2009-12-17T15:46:24.560 に答える
2

Jason の ScrollViewer トリックは、TreeViewItem を特定の位置に移動する優れた方法です。

ただし、問題が 1 つあります。MVVM では、ビュー モデルの ScrollViewer にアクセスできません。とにかくそこにたどり着く方法があります。TreeViewItem がある場合は、埋め込まれた ScrollViewer に到達するまで、そのビジュアル ツリーをたどることができます。

// Get the TreeView's ScrollViewer
DependencyObject parent = VisualTreeHelper.GetParent(selectedTreeViewItem);
while (parent != null && !(parent is ScrollViewer))
{
    parent = VisualTreeHelper.GetParent(parent);
}
于 2010-09-21T20:33:26.073 に答える