2009-12-17 20 views
5

के भीतर चयनित Treeviewitem पर स्क्रॉल करना मेरे पास एक वृक्षदृश्य लपेटने वाला स्क्रॉलव्यूयर है।स्क्रॉलव्यू

मैं वृक्षारोपण प्रोग्रामेटिक रूप से पॉप्युलेट करता हूं (यह बाध्य नहीं है), और वृक्षदृश्य को पूर्व निर्धारित वृक्षदृश्य में विस्तारित करें। यह सब ठीक काम करता है।

मेरी समस्या यह है कि जब पेड़ फैलता है तो मुझे स्क्रॉलव्यू चाहिए कि माता-पिता वृक्षदृश्य पेड़दृश्य को स्क्रॉल करने के लिए वृक्षारोपण करते हैं जिसे मैंने अभी विस्तारित किया है। कोई विचार? - ध्यान रखें कि वृक्षदृश्य में प्रत्येक बार विस्तारित होने पर एक ही संरचना नहीं हो सकती है, ताकि मौजूदा स्क्रॉल स्थिति को संग्रहीत करने और उस पर रीसेट करने का नियम हो ...

उत्तर

4

मेरे पास एक ही समस्या है, 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; 
    } 
} 
+0

धन्यवाद: आप एक TreeViewItem है, तो आप जब तक आप एम्बेडेड ScrollViewer तक पहुँचने के अपने दृश्य पेड़ पर चल सकता है! यह बहुत अच्छा काम किया। पता होना चाहिए कि वस्तुओं को पहले "कल्पना" करने की आवश्यकता है! –

+0

ऐसा लगता है कि ऑफसेट। वाई सापेक्ष है, इसलिए myScroll.ScrollToVerticalOffset (offset.Y); sv.ScrollToVerticalOffset (ऑफसेट.वाय + sv.VerticalOffset) में बदलने की जरूरत है; –

2

जेसन के ScrollViewer चाल एक विशिष्ट स्थान के लिए एक TreeViewItem हिलाने की एक शानदार तरीका है यहाँ कोड है।

एक समस्या, हालांकि: एमवीवीएम में आपको दृश्य मॉडल में स्क्रॉलव्यूयर तक पहुंच नहीं है। वैसे भी इसे पाने का एक तरीका है।

// Get the TreeView's ScrollViewer 
DependencyObject parent = VisualTreeHelper.GetParent(selectedTreeViewItem); 
while (parent != null && !(parent is ScrollViewer)) 
{ 
    parent = VisualTreeHelper.GetParent(parent); 
} 
संबंधित मुद्दे