मेरे पास एक ही समस्या है, TreeView चयनित आइटम पर स्क्रॉल नहीं कर रहा है।
मैं था क्या किया था, चयनित TreeViewItem के पेड़ के विस्तार के बाद, मैं एक डिस्पैचर सहायक विधि कहा जाता यूआई अद्यतन करने के लिए अनुमति देने के लिए, और फिर चयनित आइटम पर 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-17 15:46:24
धन्यवाद: आप एक TreeViewItem है, तो आप जब तक आप एम्बेडेड ScrollViewer तक पहुँचने के अपने दृश्य पेड़ पर चल सकता है! यह बहुत अच्छा काम किया। पता होना चाहिए कि वस्तुओं को पहले "कल्पना" करने की आवश्यकता है! –
ऐसा लगता है कि ऑफसेट। वाई सापेक्ष है, इसलिए myScroll.ScrollToVerticalOffset (offset.Y); sv.ScrollToVerticalOffset (ऑफसेट.वाय + sv.VerticalOffset) में बदलने की जरूरत है; –