Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <Button ...>
- <i:Interaction.Behaviors>
- <local:MyBehavior />
- </i:Interaction.Behaviors>
- </Button>
- <Grid>
- <Grid.Resources>
- <sys:String x:Key="stringResource1">stringResource1</sys:String>
- <local:Triggers x:Key="debugTriggers" x:Shared="False">
- <i:EventTrigger EventName="MouseLeftButtonDown">
- <local:DebugAction Message="DataContext: {0}" MessageParameter="{Binding}"/>
- <local:DebugAction Message="ElementName: {0}" MessageParameter="{Binding Text, ElementName=textBlock2}"/>
- <local:DebugAction Message="Mentor: {0}" MessageParameter="{Binding Text, RelativeSource={RelativeSource AncestorType={x:Type FrameworkElement}}}"/>
- </i:EventTrigger>
- </local:Triggers>
- <Style x:Key="debugBehavior" TargetType="FrameworkElement">
- <Setter Property="local:SupplementaryInteraction.Triggers" Value="{StaticResource debugTriggers}"/>
- </Style>
- </Grid.Resources>
- <StackPanel DataContext="{StaticResource stringResource1}">
- <TextBlock Name="textBlock1" Text="textBlock1" Style="{StaticResource debugBehavior}"/>
- <TextBlock Name="textBlock2" Text="textBlock2" Style="{StaticResource debugBehavior}"/>
- <TextBlock Name="textBlock3" Text="textBlock3" Style="{StaticResource debugBehavior}"/>
- </StackPanel>
- </Grid>
- public class DebugAction : TriggerAction<DependencyObject>
- {
- public string Message
- {
- get { return (string)GetValue(MessageProperty); }
- set { SetValue(MessageProperty, value); }
- }
- public static readonly DependencyProperty MessageProperty =
- DependencyProperty.Register("Message", typeof(string), typeof(DebugAction), new UIPropertyMetadata(""));
- public object MessageParameter
- {
- get { return (object)GetValue(MessageParameterProperty); }
- set { SetValue(MessageParameterProperty, value); }
- }
- public static readonly DependencyProperty MessageParameterProperty =
- DependencyProperty.Register("MessageParameter", typeof(object), typeof(DebugAction), new UIPropertyMetadata(null));
- protected override void Invoke(object parameter)
- {
- Debug.WriteLine(Message, MessageParameter, AssociatedObject, parameter);
- }
- }
- public class Behaviors : List<Behavior>
- {
- }
- public class Triggers : List<TriggerBase>
- {
- }
- public static class SupplementaryInteraction
- {
- public static Behaviors GetBehaviors(DependencyObject obj)
- {
- return (Behaviors)obj.GetValue(BehaviorsProperty);
- }
- public static void SetBehaviors(DependencyObject obj, Behaviors value)
- {
- obj.SetValue(BehaviorsProperty, value);
- }
- public static readonly DependencyProperty BehaviorsProperty =
- DependencyProperty.RegisterAttached("Behaviors", typeof(Behaviors), typeof(SupplementaryInteraction), new UIPropertyMetadata(null, OnPropertyBehaviorsChanged));
- private static void OnPropertyBehaviorsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- var behaviors = Interaction.GetBehaviors(d);
- foreach (var behavior in e.NewValue as Behaviors) behaviors.Add(behavior);
- }
- public static Triggers GetTriggers(DependencyObject obj)
- {
- return (Triggers)obj.GetValue(TriggersProperty);
- }
- public static void SetTriggers(DependencyObject obj, Triggers value)
- {
- obj.SetValue(TriggersProperty, value);
- }
- public static readonly DependencyProperty TriggersProperty =
- DependencyProperty.RegisterAttached("Triggers", typeof(Triggers), typeof(SupplementaryInteraction), new UIPropertyMetadata(null, OnPropertyTriggersChanged));
- private static void OnPropertyTriggersChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- var triggers = Interaction.GetTriggers(d);
- foreach (var trigger in e.NewValue as Triggers) triggers.Add(trigger);
- }
- }
- #region Attached Properties Boilerplate
- public static readonly DependencyProperty IsActiveProperty = DependencyProperty.RegisterAttached("IsActive", typeof(bool), typeof(ScrollIntoViewBehavior), new PropertyMetadata(false, OnIsActiveChanged));
- public static bool GetIsActive(FrameworkElement control)
- {
- return (bool)control.GetValue(IsActiveProperty);
- }
- public static void SetIsActive(
- FrameworkElement control, bool value)
- {
- control.SetValue(IsActiveProperty, value);
- }
- private static void OnIsActiveChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- var behaviors = Interaction.GetBehaviors(d);
- var newValue = (bool)e.NewValue;
- if (newValue)
- {
- //add the behavior if we don't already have one
- if (!behaviors.OfType<ScrollIntoViewBehavior>().Any())
- {
- behaviors.Add(new ScrollIntoViewBehavior());
- }
- }
- else
- {
- //remove any instance of the behavior. (There should only be one, but just in case.)
- foreach (var item in behaviors.ToArray())
- {
- if (item is ScrollIntoViewBehavior)
- behaviors.Remove(item);
- }
- }
- }
- #endregion
- <Style TargetType="Button">
- <Setter Property="Blah:ScrollIntoViewBehavior.IsActive" Value="True" />
- </Style>
Add Comment
Please, Sign In to add comment