2010-01-19 61 views
5

我在Silverlight 3.0中有一個View和一個ViewModel。ScrollViewer上的VerticalOffset屬性的雙向綁定?

該視圖包含一個標準的ScrollViewer,其中包含動態內容。

根據ScrollViewer中的內容,用戶可能會滾動到內容的中間位置,然後執行一個操作,導致ScrollViewer加載新內容,但ScrollViewer不會自動滾動到頂部。

我希望能夠綁定到VerticalOffset屬性,但它是隻讀的。關於可附着行爲的任何想法? 任何想法?

謝謝。

+0

要公開在視圖模型一個屬性,它指示了ScrollViewer應該是?它不清楚你想要將VerticalOffset綁定到什麼? – AnthonyWJones 2010-01-19 21:24:53

回答

3

由於您使用的是視圖模型我把它叫做「行動導致的ScrollViewer加載新的內容」裏面或視圖模型所做的更改的結果。在這種情況下,我會向ViewModel添加一個事件,每次發生這種變化時都會觸發該事件。此事件

您的視圖可以添加一個處理程序,並調用ScrollToVerticalPosition在ScrollViewer中其發射時。

4

以下博客文章提供了一個附加的行爲,它公開的ScrollViewer的垂直/水平偏移,以便可以結合到它們,或將它們設置在代碼:

http://blog.scottlogic.com/2010/07/21/exposing-and-binding-to-a-silverlight-scrollviewers-scrollbars.html

這允許以下標記:

<ScrollViewer 
    local:ScrollViewerBinding.VerticalOffset="{Binding YPosition, Mode=TwoWay}" 
    local:ScrollViewerBinding.HorizontalOffset="{Binding XPosition, Mode=TwoWay}"> 
    <!-- Big content goes here! --> 
</ScrollViewer> 
+0

我想看一篇文章(我需要wpf解決方案來綁定scrollviewer),但鏈接是錯誤的。 – Sinatr 2016-02-01 13:48:16

+0

http://blog.scottlogic.com/2010/07/21/exposing-and-binding-to-a-silverlight-scrollviewers-scrollbars.html – Thomas 2016-02-11 14:00:14

+0

@Thomas感謝 - 我已經更新的鏈接 – ColinE 2016-02-11 17:18:58

0

我簡化了@ColinE的解決方案。而不是掛在ScrollBar.ValueChanged事件,我鉤到ScrollViewer.ScrollChanged事件。因此,1。沒有必要找到在視覺樹ScrollBar和2 ScrollBar.ValueChanged就是所謂的一些過渡態時的ScrollViewer變化的內容和我不希望趕上這些國家。

我後我的代碼爲VerticalOffset,該HorizontalOffset是相似的:

/// <summary> 
/// VerticalOffset attached property 
/// </summary> 
public static readonly DependencyProperty VerticalOffsetProperty = 
    DependencyProperty.RegisterAttached("VerticalOffset", typeof(double), 
    typeof(ScrollViewerBinding), new FrameworkPropertyMetadata(double.NaN, 
     FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, 
     OnVerticalOffsetPropertyChanged)); 
      OnVerticalOffsetPropertyChanged)); 

/// <summary> 
/// Just a flag that the binding has been applied. 
/// </summary> 
private static readonly DependencyProperty VerticalScrollBindingProperty = 
    DependencyProperty.RegisterAttached("VerticalScrollBinding", typeof(bool?), typeof(ScrollViewerBinding)); 

public static double GetVerticalOffset(DependencyObject depObj) 
{ 
    return (double)depObj.GetValue(VerticalOffsetProperty); 
} 

public static void SetVerticalOffset(DependencyObject depObj, double value) 
{ 
    depObj.SetValue(VerticalOffsetProperty, value); 
} 

private static void OnVerticalOffsetPropertyChanged(DependencyObject d, 
    DependencyPropertyChangedEventArgs e) 
{ 
    ScrollViewer scrollViewer = d as ScrollViewer; 
    if (scrollViewer == null) 
     return; 

    BindVerticalOffset(scrollViewer); 
    scrollViewer.ScrollToVerticalOffset((double)e.NewValue); 
} 

public static void BindVerticalOffset(ScrollViewer scrollViewer) 
{ 
    if (scrollViewer.GetValue(VerticalScrollBindingProperty) != null) 
     return; 

    scrollViewer.SetValue(VerticalScrollBindingProperty, true); 
    scrollViewer.ScrollChanged += (s, se) => 
    { 
     if (se.VerticalChange == 0) 
      return; 
     SetVerticalOffset(scrollViewer, se.VerticalOffset); 
    }; 
} 

而且用它在XAML:

<ScrollViewer local:ScrollViewerBinding.VerticalOffset="{Binding ScrollVertical}"> 
    <!-- content ... --> 
</ScrollViewer> 
相關問題