2010-12-02 9 views
5

मेरे पास वर्तमान में दो स्क्रॉलव्यूयर के समान संग्रह के वैकल्पिक दृश्य हैं। मैंने ScrollChanged ईवेंट को संभालने और ScrollToVerticalOffset का उपयोग करके दो स्क्रॉलव्यूर्स की स्क्रॉलिंग को बाध्य किया है।स्क्रॉलबार और स्क्रॉलव्यूयर को कैसे लिंक करें

प्रस्तुति के कारणों से मैंने स्क्रॉलव्यूयर स्क्रॉलबार दोनों को छुपाया है और उन्हें अलग स्क्रॉलबार से नियंत्रित करना चाहते हैं।

ऐसा लगता है कि यह सीधा नहीं है। मुझे कुछ महीने पहले इसके बारे में एक ब्लॉग देखने को याद आया लेकिन मुझे इसे फिर से नहीं मिला।

क्या कोई मुझे कुछ उपयोगी संसाधनों की दिशा में इंगित कर सकता है या मुझे सही दिशा में एक झुंड दे सकता है कि यह कैसे प्राप्त किया जा सकता है।

अग्रिम धन्यवाद।

उत्तर

7

ठीक है, इसे हल किया गया। वास्तव में काफी सरल था।

तब से Wpf binding to a function मिला है, जो किसी और को रुचि रखने में मदद कर सकता है। इसका वीबी लेकिन पर्याप्त स्पष्ट होना चाहिए।

चीयर्स

ऊपर करने के लिए आगे: मैं स्क्रॉलपट्टी subclassed और ScrollViewer मैं बाध्य करने के लिए करना चाहता था में पारित कर दिया। ठीक काम करने लगता है।

public class ScrollViewerBoundScrollBar : ScrollBar 
{ 
    private ScrollViewer _scrollViewer; 
    public ScrollViewer BoundScrollViewer { get { return _scrollViewer; } set { _scrollViewer = value; UpdateBindings(); } } 

    public ScrollViewerBoundScrollBar(ScrollViewer scrollViewer, Orientation o) : base() 
    { 
     this.Orientation = o; 
     BoundScrollViewer = _scrollViewer; 
    } 

    public ScrollViewerBoundScrollBar() : base() 
    { 
    } 

    private void UpdateBindings() 
    { 
     this.AddHandler(ScrollBar.ScrollEvent, new ScrollEventHandler(OnScroll)); 
     _scrollViewer.AddHandler(ScrollViewer.ScrollChangedEvent, new ScrollChangedEventHandler(BoundScrollChanged)); 
     this.Minimum = 0; 
     if (Orientation == Orientation.Horizontal) 
     { 
      this.SetBinding(ScrollBar.MaximumProperty, (new Binding("ScrollableWidth") { Source = _scrollViewer, Mode = BindingMode.OneWay })); 
      this.SetBinding(ScrollBar.ViewportSizeProperty, (new Binding("ViewportWidth") { Source = _scrollViewer, Mode = BindingMode.OneWay })); 
     } 
     else 
     { 
      this.SetBinding(ScrollBar.MaximumProperty, (new Binding("ScrollableHeight") { Source = _scrollViewer, Mode = BindingMode.OneWay })); 
      this.SetBinding(ScrollBar.ViewportSizeProperty, (new Binding("ViewportHeight") { Source = _scrollViewer, Mode = BindingMode.OneWay })); 
     } 
     this.LargeChange = 242; 
     this.SmallChange = 16; 
    } 

    public void BoundScrollChanged(object sender, ScrollChangedEventArgs e) 
    { 
     switch (this.Orientation) 
     { 
      case Orientation.Horizontal: 
       this.Value = e.HorizontalOffset; 
       break; 
      case Orientation.Vertical: 
       this.Value = e.VerticalOffset; 
       break; 
      default: 
       break; 
     } 
    } 

    public void OnScroll(object sender, ScrollEventArgs e) 
    { 
     switch(this.Orientation) 
     { 
      case Orientation.Horizontal: 
       this.BoundScrollViewer.ScrollToHorizontalOffset(e.NewValue); 
       break; 
      case Orientation.Vertical: 
       this.BoundScrollViewer.ScrollToVerticalOffset(e.NewValue); 
       break; 
      default: 
       break; 
     } 
    } 
} 
+0

+1 पूरी तरह से काम किया। धन्यवाद! –

11

ग्रेट, बस मुझे जो चाहिए था। मैंने थोड़ी सी विस्तार की ताकि स्क्रॉलव्यूवर को एक्सएमएल से सेट किया जा सके और साथ ही एक निर्भरता संपत्ति का उपयोग किया जा सके। XAML में:

<local:BindableScrollBar BoundScrollViewer ="{Binding ElementName=ScrollViewer}" Orientation="Vertical" /> 

कोड:

/// <summary> 
/// An extended scrollbar that can be bound to an external scrollviewer. 
/// </summary> 
public class BindableScrollBar : ScrollBar 
{ 
    public ScrollViewer BoundScrollViewer 
    { 
     get { return (ScrollViewer)GetValue(BoundScrollViewerProperty); } 
     set { SetValue(BoundScrollViewerProperty, value); } 
    } 

    // Using a DependencyProperty as the backing store for BoundScrollViewer. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty BoundScrollViewerProperty = 
     DependencyProperty.Register("BoundScrollViewer", typeof(ScrollViewer), typeof(BindableScrollBar), new FrameworkPropertyMetadata(null, new PropertyChangedCallback(OnBoundScrollViewerPropertyChanged))); 

    private static void OnBoundScrollViewerPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     BindableScrollBar sender = d as BindableScrollBar; 
     if (sender != null && e.NewValue != null) 
     { 
      sender.UpdateBindings(); 
     } 
    } 



    /// <summary> 
    /// Initializes a new instance of the <see cref="BindableScrollBar"/> class. 
    /// </summary> 
    /// <param name="scrollViewer">The scroll viewer.</param> 
    /// <param name="o">The o.</param> 
    public BindableScrollBar(ScrollViewer scrollViewer, Orientation o) 
     : base() 
    { 
     this.Orientation = o; 
     BoundScrollViewer = scrollViewer; 
    } 

    /// <summary> 
    /// Initializes a new instance of the <see cref="BindableScrollBar"/> class. 
    /// </summary> 
    public BindableScrollBar() : base() { } 

    private void UpdateBindings() 
    { 
     AddHandler(ScrollBar.ScrollEvent, new ScrollEventHandler(OnScroll)); 
     BoundScrollViewer.AddHandler(ScrollViewer.ScrollChangedEvent, new ScrollChangedEventHandler(BoundScrollChanged)); 
     Minimum = 0; 
     if (Orientation == Orientation.Horizontal) 
     { 
      SetBinding(ScrollBar.MaximumProperty, (new Binding("ScrollableWidth") { Source = BoundScrollViewer, Mode = BindingMode.OneWay })); 
      SetBinding(ScrollBar.ViewportSizeProperty, (new Binding("ViewportWidth") { Source = BoundScrollViewer, Mode = BindingMode.OneWay })); 
     } 
     else 
     { 
      this.SetBinding(ScrollBar.MaximumProperty, (new Binding("ScrollableHeight") { Source = BoundScrollViewer, Mode = BindingMode.OneWay })); 
      this.SetBinding(ScrollBar.ViewportSizeProperty, (new Binding("ViewportHeight") { Source = BoundScrollViewer, Mode = BindingMode.OneWay })); 
     } 
     LargeChange = 242; 
     SmallChange = 16; 
    } 

    private void BoundScrollChanged(object sender, ScrollChangedEventArgs e) 
    { 
     switch (this.Orientation) 
     { 
      case Orientation.Horizontal: 
       this.Value = e.HorizontalOffset; 
      break; 
      case Orientation.Vertical: 
       this.Value = e.VerticalOffset; 
       break; 
      default: 
       break; 
     } 
    } 

    private void OnScroll(object sender, ScrollEventArgs e) 
    { 
     switch (this.Orientation) 
     { 
      case Orientation.Horizontal: 
       this.BoundScrollViewer.ScrollToHorizontalOffset(e.NewValue); 
       break; 
      case Orientation.Vertical: 
       this.BoundScrollViewer.ScrollToVerticalOffset(e.NewValue); 
       break; 
      default: 
       break; 
     } 
    } 
} 
+0

धन्यवाद! यह एक आकर्षण की तरह काम करता है :) – Arseny

+0

टेम्पलेट लागू होने के बाद मैं टेम्पलेट आइटम पर बाइंडेबलक्रॉलबार को कैसे बांध सकता हूं? मेरा अन्य स्क्रॉलव्यूअर तब तक अस्तित्व में नहीं है जब बाइंडेबल स्क्रॉलबार बनाया गया है, इसलिए आइटम बाध्य नहीं है। –

+0

+1 आप लोग चट्टान! –

संबंधित मुद्दे