Click here to Skip to main content
12,254,393 members (60,625 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

41.1K views
14 bookmarked
Posted

Friction Scrolling Now An WPF Attached Behaviour Too

, 29 Dec 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
A while ago I wrote about how to create a scrollable design surface in WPF, and how you could also add friction into the mix.My original post was called “Creating A Scrollable Control Surface In WPF” which can be found at  the following url:http://sachabarber.net/?p=225This original blog post p

A while ago I wrote about how to create a scrollable design surface in WPF, and how you could also add friction into the mix.

My original post was called “Creating A Scrollable Control Surface In WPF” which can be found at  the following url:

http://sachabarber.net/?p=225

This original blog post proved to be quite popular and one of my fellow WPF Disciples my homeboy Jeremiah Morrill took it upon himself to rewrite my little control to be a content control for Silverlight, which you can get to at  “Scrollable Friction Canvas For Silverlight” which can be found at  the following url:

http://sachabarber.net/?p=481

I have been asked for my original code a lot, and another of my friends, and founder of the WPF Disciples, Marlon Grech took my code and has further improved it for WPF users, by making it an attached behaviour so all you have to do is hook up one property on your ScrollViewer and bingo its a Friction enabled surface. Neato I say.

Here is Marlons attached behaviour code:

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Text;
   5:  using System.Windows;
   6:  using System.Windows.Controls;
   7:  using System.Windows.Input;
   8:  using System.Windows.Threading;
   9:   
  10:  namespace ScrollableArea
  11:  {
  12:      public class KineticBehaviour
  13:      {
  14:          #region Friction
  15:   
  16:          /// <span class="code-SummaryComment"><summary>
</span>

Which to use you would simply do this :

 1:  <Window x:Class="ScrollableArea.Window1"
 2:      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3:      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4:      xmlns:local="clr-namespace:ScrollableArea"
 5:      Title="Window1" Height="300" Width="300">
 6:      <Window.Resources>
 7:
 8:          <!-- scroll viewer -->
 9:          <Style x:Key="ScrollViewerStyle"
10:                 TargetType="{x:Type ScrollViewer}">
11:              <Setter Property="HorizontalScrollBarVisibility"
12:                      Value="Hidden" />
13:              <Setter Property="VerticalScrollBarVisibility"
14:                      Value="Hidden" />
15:          </Style>
16:
17:      </Window.Resources>
18:
19:      <Grid Margin="0">
20:          <ScrollViewer x:Name="ScrollViewer"
21:              Style="{StaticResource ScrollViewerStyle}"
22:              local:KineticBehaviour.HandleKineticScrolling="True">
23:              <ItemsControl x:Name="itemsControl"
24:                            VerticalAlignment="Center">
25:
26:                  <ItemsControl.ItemsPanel>
27:                      <ItemsPanelTemplate>
28:                          <!-- Custom Panel-->
29:                          <StackPanel Orientation="Vertical"/>
30:                      </ItemsPanelTemplate>
31:                  </ItemsControl.ItemsPanel>
32:
33:
34:              </ItemsControl>
35:          </ScrollViewer>
36:      </Grid>
37:
38:  </Window>

As always here is a small demo app:

http://dl.dropbox.com/u/2600965/ScrollableAreaAttachedBehaviour.zip

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Sacha Barber
Software Developer (Senior)
United Kingdom United Kingdom
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)

- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence

Both of these at Sussex University UK.

Award(s)

I am lucky enough to have won a few awards for Zany Crazy code articles over the years

  • Microsoft C# MVP 2016
  • Codeproject MVP 2016
  • Microsoft C# MVP 2015
  • Codeproject MVP 2015
  • Microsoft C# MVP 2014
  • Codeproject MVP 2014
  • Microsoft C# MVP 2013
  • Codeproject MVP 2013
  • Microsoft C# MVP 2012
  • Codeproject MVP 2012
  • Microsoft C# MVP 2011
  • Codeproject MVP 2011
  • Microsoft C# MVP 2010
  • Codeproject MVP 2010
  • Microsoft C# MVP 2009
  • Codeproject MVP 2009
  • Microsoft C# MVP 2008
  • Codeproject MVP 2008
  • And numerous codeproject awards which you can see over at my blog

You may also be interested in...

Comments and Discussions

 
QuestionCan you optimize it please? Pin
qakmak7-Dec-15 5:45
memberqakmak7-Dec-15 5:45 
QuestionWhy don't you use animation replace timer? Pin
qakmak24-Nov-15 0:13
memberqakmak24-Nov-15 0:13 
AnswerRe: Why don't you use animation replace timer? Pin
qakmak24-Nov-15 2:40
memberqakmak24-Nov-15 2:40 
GeneralRe: Why don't you use animation replace timer? Pin
Sacha Barber24-Nov-15 3:37
mvpSacha Barber24-Nov-15 3:37 
GeneralRe: Why don't you use animation replace timer? Pin
qakmak26-Nov-15 0:46
memberqakmak26-Nov-15 0:46 
QuestionFlowDocumentScroll Viewer Pin
sharathkumar112-Oct-12 23:19
membersharathkumar112-Oct-12 23:19 
QuestionListBox. Pin
Brummell224-Aug-12 12:10
memberBrummell224-Aug-12 12:10 
AnswerRe: ListBox. Pin
Sacha Barber24-Aug-12 16:52
mvpSacha Barber24-Aug-12 16:52 
GeneralRe: ListBox. Pin
Brummell224-Aug-12 17:35
memberBrummell224-Aug-12 17:35 
GeneralRe: ListBox. Pin
qakmak24-Nov-15 0:11
memberqakmak24-Nov-15 0:11 
GeneralMy vote of 3 Pin
buyong27-Sep-11 17:17
memberbuyong27-Sep-11 17:17 
GeneralMy vote of 3 Pin
mheskol1-Feb-11 6:29
membermheskol1-Feb-11 6:29 
GeneralRe: My vote of 3 Pin
MmPizza5-Apr-12 6:33
memberMmPizza5-Apr-12 6:33 
QuestionQuestion Pin
stefan++15-Feb-10 23:08
memberstefan++15-Feb-10 23:08 
AnswerRe: Question Pin
Sacha Barber16-Feb-10 0:25
mvpSacha Barber16-Feb-10 0:25 
GeneralRe: Question Pin
stefan++16-Feb-10 3:16
memberstefan++16-Feb-10 3:16 
GeneralRe: Question Pin
Sacha Barber16-Feb-10 3:19
mvpSacha Barber16-Feb-10 3:19 
GeneralControls in the scrollviewer Pin
bflosabre9129-Dec-09 9:15
memberbflosabre9129-Dec-09 9:15 
GeneralRe: Controls in the scrollviewer Pin
Sacha Barber29-Dec-09 20:21
mvpSacha Barber29-Dec-09 20:21 
GeneralRe: Controls in the scrollviewer Pin
Sacha Barber29-Dec-09 20:31
mvpSacha Barber29-Dec-09 20:31 
GeneralRe: Controls in the scrollviewer Pin
bflosabre917-Jan-10 4:55
memberbflosabre917-Jan-10 4:55 
GeneralRe: Controls in the scrollviewer Pin
Sacha Barber7-Jan-10 5:01
mvpSacha Barber7-Jan-10 5:01 
GeneralRe: Controls in the scrollviewer Pin
bflosabre917-Jan-10 11:15
memberbflosabre917-Jan-10 11:15 
GeneralRe: Controls in the scrollviewer Pin
Sacha Barber7-Jan-10 23:11
mvpSacha Barber7-Jan-10 23:11 
GeneralRe: Controls in the scrollviewer Pin
Jean-Francois Dionne8-Nov-10 0:12
memberJean-Francois Dionne8-Nov-10 0:12 
AnswerRe: Controls in the scrollviewer [modified] Pin
efrost27-May-10 20:53
memberefrost27-May-10 20:53 
GeneralRe: Controls in the scrollviewer [modified] Pin
efrost27-May-10 22:42
memberefrost27-May-10 22:42 
GeneralRe: Controls in the scrollviewer Pin
Sacha Barber28-May-10 0:00
mvpSacha Barber28-May-10 0:00 
GeneralRe: Controls in the scrollviewer Pin
qakmak24-Nov-15 0:15
memberqakmak24-Nov-15 0:15 
GeneralRe: Controls in the scrollviewer Pin
shuixingzhiyi22-Apr-11 21:21
membershuixingzhiyi22-Apr-11 21:21 
GeneralRe: Controls in the scrollviewer [modified] Pin
sharathkumar1110-Oct-12 22:19
membersharathkumar1110-Oct-12 22:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160426.1 | Last Updated 29 Dec 2009
Article Copyright 2009 by Sacha Barber
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid