2013-02-26 83 views
0

是否可以從受保護的override override void取消訂閱事件處理程序?取消訂閱來自Protected Override Void的事件處理程序

protected override void OnViewLoaded(object sender, ViewLoadedEventArg e) 
    { 
     base.OnViewLoaded(sender, e); 
     list = VisualTreeUtil.FindFirstInTree<ListView>(Application.Current.MainWindow, "ListView"); 
     ConfigureAndSuperviseInputControls(this.list); 
     ScrollViewer scroll = VisualTreeUtil.FindFirstInTree<ScrollViewer>(this.list); 
     scroll.ScrollChanged+=new ScrollChangedEventHandler(scroll_ScrollChanged);  
    } 

    void scroll_ScrollChanged(object sender, ScrollChangedEventArgs e) 
    { 
     ConfigureAndSuperviseInputControls(this.list); 
     ScrollViewer sb = e.OriginalSource as ScrollViewer; 
     if (sb.ContentVerticalOffset==sb.ScrollableHeight) 
     { 
      scroll.ScrollChanged-=new ScrollChangedEventHandler(scroll_ScrollChanged); 
     } 
    } 

我的問題是,我沒有得到訪問scroll_ScrollChanged方法中的滾動obejct。

+2

是不是發送到對象的引用,你想? – Andy 2013-02-26 10:13:38

回答

0

此代碼未經測試,但你不能簡單的鑄件發送對象到ScrollViewer中,並從這樣的事件退訂:

protected override void OnViewLoaded(object sender, ViewLoadedEventArg e) 
    { 
     base.OnViewLoaded(sender, e); 
     list = VisualTreeUtil.FindFirstInTree<ListView>(Application.Current.MainWindow, "ListView"); 
     ConfigureAndSuperviseInputControls(this.list); 
     ScrollViewer scroll = VisualTreeUtil.FindFirstInTree<ScrollViewer>(this.list); 
     scroll.ScrollChanged+=new ScrollChangedEventHandler(scroll_ScrollChanged);  
    } 

    void scroll_ScrollChanged(object sender, ScrollChangedEventArgs e) 
    { 
     ConfigureAndSuperviseInputControls(this.list); 
     ScrollViewer scroll = (ScrollViewer)sender; 
     if (scroll.ContentVerticalOffset==scroll.ScrollableHeight) 
     { 
      scroll.ScrollChanged-=new ScrollChangedEventHandler(scroll_ScrollChanged); 
     } 
    } 
+0

完美!這很好,謝謝 – Bulli 2013-02-26 10:23:22

0

發件人應該是您需要的對象的引用。

protected override void OnViewLoaded(object sender, ViewLoadedEventArg e) 
    { 
     base.OnViewLoaded(sender, e); 
     list = VisualTreeUtil.FindFirstInTree<ListView>(Application.Current.MainWindow, "ListView"); 
     ConfigureAndSuperviseInputControls(this.list); 
     ScrollViewer scroll = VisualTreeUtil.FindFirstInTree<ScrollViewer>(this.list); 
     scroll.ScrollChanged+=new ScrollChangedEventHandler(scroll_ScrollChanged);  
    } 

    void scroll_ScrollChanged(object sender, ScrollChangedEventArgs e) 
    { 
     ConfigureAndSuperviseInputControls(this.list); 
     ScrollViewer sb = sender as ScrollViewer; 
     if (sb.ContentVerticalOffset==sb.ScrollableHeight) 
     { 
      scroll.ScrollChanged-=new ScrollChangedEventHandler(scroll_ScrollChanged); 
     } 
    } 
+0

這似乎花了我太多的時間來寫我的答案;) – dasheddot 2013-02-26 10:18:25

+0

發件人作爲參考沒有任何變化 – Bulli 2013-02-26 10:19:50

+0

斷點行「if(sb.ContentVerticalOffset == sb.ScrollableHeight)」,並看看發件人是否有引用ScrollViewer – Andy 2013-02-26 10:23:37

相關問題