Created
February 11, 2012 04:10
-
-
Save AlbertoMonteiro/1796090 to your computer and use it in GitHub Desktop.
Custom WP7 Behaviors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Windows; | |
using System.Windows.Controls; | |
using System.Windows.Interactivity; | |
namespace Custom.Behaviors | |
{ | |
public class ScrollPositionBehavior : Behavior<ScrollViewer> | |
{ | |
public double Position | |
{ | |
get { return (double)GetValue(PositionProperty); } | |
set { SetValue(PositionProperty, value); } | |
} | |
public static readonly DependencyProperty PositionProperty = | |
DependencyProperty.Register("Position", typeof(double), typeof(ScrollPositionBehavior), new PropertyMetadata((double)0, OnPositionChanged)); | |
private ScrollViewer scrollViewer; | |
protected override void OnAttached() | |
{ | |
base.OnAttached(); | |
scrollViewer = AssociatedObject; | |
scrollViewer.LayoutUpdated += (sender, args) => | |
{ | |
Position = scrollViewer.VerticalOffset; | |
}; | |
} | |
private static void OnPositionChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) | |
{ | |
var behavior = (ScrollPositionBehavior)sender; | |
var value = (double)e.NewValue; | |
if (Math.Abs(value - 0) < 0.00001) | |
{ | |
behavior.AssociatedObject.ScrollToVerticalOffset(value); | |
} | |
} | |
protected override void OnDetaching() | |
{ | |
base.OnDetaching(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Windows; | |
using System.Windows.Controls; | |
using System.Windows.Interactivity; | |
using Microsoft.Phone.Controls; | |
namespace Custom.Behaviors | |
{ | |
public class TrackablePivotBehavior : Behavior<Pivot> | |
{ | |
public static readonly DependencyProperty SelectedIndexProperty = | |
DependencyProperty.Register("SelectedIndex", typeof(int), typeof(TrackablePivotBehavior), new PropertyMetadata(0, SelectedIndexPropertyChanged)); | |
private Pivot panarama; | |
public int SelectedIndex | |
{ | |
get { return (int)GetValue(SelectedIndexProperty); } | |
set { SetValue(SelectedIndexProperty, value); } | |
} | |
private static void SelectedIndexPropertyChanged(DependencyObject dpObj, DependencyPropertyChangedEventArgs change) | |
{ | |
if (change.NewValue.GetType() != typeof(int) || dpObj.GetType() != typeof(TrackablePivotBehavior)) | |
return; | |
var track = (TrackablePivotBehavior)dpObj; | |
Pivot pan = track.panarama; | |
var index = (int)change.NewValue; | |
if (pan.Items.Count > index) | |
pan.SelectedIndex = (int)change.NewValue; | |
} | |
protected override void OnAttached() | |
{ | |
base.OnAttached(); | |
panarama = AssociatedObject; | |
panarama.SelectionChanged += PanaramaSelectionChanged; | |
} | |
protected override void OnDetaching() | |
{ | |
base.OnDetaching(); | |
if (panarama != null) | |
panarama.SelectionChanged += PanaramaSelectionChanged; | |
} | |
private void PanaramaSelectionChanged(object sender, SelectionChangedEventArgs e) | |
{ | |
SelectedIndex = panarama.SelectedIndex; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment