मैं अब थोड़ी देर के लिए एक आइटम के लिए स्क्रॉल करने के लिए एक VirtualizingStackPanel के साथ एक ItemsControl हो रही पर देख रहा है, और खोजने रखा "एक सूची बॉक्स का प्रयोग करें" उत्तर। मैं नहीं चाहता था, इसलिए मुझे ऐसा करने का एक तरीका मिला। सबसे पहले आपको अपने आइटम्स कंट्रोल के लिए एक नियंत्रण टेम्पलेट सेट अप करने की आवश्यकता है जिसमें उसमें स्क्रोलव्यूयर है (जो कि आपके पास पहले से ही है यदि आप आइटम नियंत्रण का उपयोग कर रहे हैं)। मेरे बुनियादी टेम्पलेट निम्नलिखित (ItemsControl के लिए एक आसान शैली में निहित)
<Style x:Key="TheItemsControlStyle" TargetType="{x:Type ItemsControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ItemsControl}">
<Border BorderThickness="{TemplateBinding Border.BorderThickness}" Padding="{TemplateBinding Control.Padding}" BorderBrush="{TemplateBinding Border.BorderBrush}" Background="{TemplateBinding Panel.Background}" SnapsToDevicePixels="True">
<ScrollViewer Padding="{TemplateBinding Control.Padding}" Focusable="False" HorizontalScrollBarVisibility="Auto">
<ItemsPresenter SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
</ScrollViewer>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
तो मैं मूल रूप से एक पुस्तक दर्शक मेरी सामग्री शामिल करने के जा रहा thats के साथ एक सीमा मिल गया है की तरह लग रहा है। मज़े की बात अब
<ItemsControl x:Name="myItemsControl" [..snip..] Style="{DynamicResource TheItemsControlStyle}" ScrollViewer.CanContentScroll="True" VirtualizingStackPanel.IsVirtualizing="True">
ठीक है:
मेरे ItemsControl साथ परिभाषित किया गया है।
public static void VirtualizedScrollIntoView(this ItemsControl control, object item) {
try {
// this is basically getting a reference to the ScrollViewer defined in the ItemsControl's style (identified above).
// you *could* enumerate over the ItemsControl's children until you hit a scroll viewer, but this is quick and
// dirty!
// First 0 in the GetChild returns the Border from the ControlTemplate, and the second 0 gets the ScrollViewer from
// the Border.
ScrollViewer sv = VisualTreeHelper.GetChild(VisualTreeHelper.GetChild((DependencyObject)control, 0), 0) as ScrollViewer;
// now get the index of the item your passing in
int index = control.Items.IndexOf(item);
if(index != -1) {
// since the scroll viewer is using content scrolling not pixel based scrolling we just tell it to scroll to the index of the item
// and viola! we scroll there!
sv.ScrollToVerticalOffset(index);
}
} catch(Exception ex) {
Debug.WriteLine("What the..." + ex.Message);
}
}
तो जगह में विस्तार विधि के साथ आप यह सिर्फ ListBox साथी विधि की तरह का प्रयोग करेंगे::
myItemsControl.VirtualizedScrollIntoView(someItemInTheList);
मैं दिए गए आइटम के लिए स्क्रॉल करने के लिए इसे पाने के लिए किसी भी ItemsControl को संलग्न करने के लिए एक विस्तार विधि बना लिया है
बढ़िया काम करता है!
ध्यान दें कि आप अपने आइटमों को प्राप्त करने के लिए sv.ScrollToEnd() और अन्य सामान्य स्क्रॉलिंग विधियों को भी कॉल कर सकते हैं।
स्रोत
2012-11-28 04:11:21
मैं ऐसा करने से बचना चाहता हूं क्योंकि मुझे 'सूची बॉक्स' की "आइटम का चयन करें" क्षमता की आवश्यकता नहीं है।कोई विचार क्यों 'ItemControl' में' ScrollIntoView' नहीं है? –
@ सेठ: जैसा कि मैंने कहा था, आप चयन छुपा सकते हैं, जो परवाह करता है अगर यह परवाह करता है? इसमें स्क्रॉलिंग नहीं है क्योंकि इसे इस तरह से डिजाइन किया गया था, 'आइटम नियंत्रण' आइटम नियंत्रणों का सबसे बुनियादी है, ऐसे बेस क्लास के लिए स्क्रॉलिंग कार्यक्षमता की आवश्यकता नहीं है। –
अब यह पता लगाने के लिए कि 'ListBox' को किसी आइटम को पूरी तरह से देखने पर कैसे स्क्रॉल करना है ... –