Tôi đã có cùng một vấn đề, với TreeView không di chuyển đến mục đã chọn.
Điều tôi đã làm là sau khi mở rộng cây tới TreeViewItem đã chọn, tôi đã gọi phương thức Bộ điều phối cho phép giao diện người dùng cập nhật và sau đó sử dụng TransformToAncestor trên mục đã chọn để tìm vị trí của nó trong ScrollViewer. Đây là mã:
// 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);
đây là đoạn code 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;
}
}
Nguồn
2009-12-17 15:46:24
Cảm ơn! Điều đó làm việc tuyệt vời. Nên biết rằng các mục cần được "hình dung" trước tiên! –
Có vẻ như offet.Y là tương đối, vì vậy myScroll.ScrollToVerticalOffset (offset.Y); cần phải được thay đổi thành sv.ScrollToVerticalOffset (offset.Y + sv.VerticalOffset); –