Guest User

Untitled

a guest
Apr 24th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.24 KB | None | 0 0
  1. <Button ...>
  2. <i:Interaction.Behaviors>
  3. <local:MyBehavior />
  4. </i:Interaction.Behaviors>
  5. </Button>
  6.  
  7. <Grid>
  8. <Grid.Resources>
  9. <sys:String x:Key="stringResource1">stringResource1</sys:String>
  10. <local:Triggers x:Key="debugTriggers" x:Shared="False">
  11. <i:EventTrigger EventName="MouseLeftButtonDown">
  12. <local:DebugAction Message="DataContext: {0}" MessageParameter="{Binding}"/>
  13. <local:DebugAction Message="ElementName: {0}" MessageParameter="{Binding Text, ElementName=textBlock2}"/>
  14. <local:DebugAction Message="Mentor: {0}" MessageParameter="{Binding Text, RelativeSource={RelativeSource AncestorType={x:Type FrameworkElement}}}"/>
  15. </i:EventTrigger>
  16. </local:Triggers>
  17. <Style x:Key="debugBehavior" TargetType="FrameworkElement">
  18. <Setter Property="local:SupplementaryInteraction.Triggers" Value="{StaticResource debugTriggers}"/>
  19. </Style>
  20. </Grid.Resources>
  21. <StackPanel DataContext="{StaticResource stringResource1}">
  22. <TextBlock Name="textBlock1" Text="textBlock1" Style="{StaticResource debugBehavior}"/>
  23. <TextBlock Name="textBlock2" Text="textBlock2" Style="{StaticResource debugBehavior}"/>
  24. <TextBlock Name="textBlock3" Text="textBlock3" Style="{StaticResource debugBehavior}"/>
  25. </StackPanel>
  26. </Grid>
  27.  
  28. public class DebugAction : TriggerAction<DependencyObject>
  29. {
  30. public string Message
  31. {
  32. get { return (string)GetValue(MessageProperty); }
  33. set { SetValue(MessageProperty, value); }
  34. }
  35.  
  36. public static readonly DependencyProperty MessageProperty =
  37. DependencyProperty.Register("Message", typeof(string), typeof(DebugAction), new UIPropertyMetadata(""));
  38.  
  39. public object MessageParameter
  40. {
  41. get { return (object)GetValue(MessageParameterProperty); }
  42. set { SetValue(MessageParameterProperty, value); }
  43. }
  44.  
  45. public static readonly DependencyProperty MessageParameterProperty =
  46. DependencyProperty.Register("MessageParameter", typeof(object), typeof(DebugAction), new UIPropertyMetadata(null));
  47.  
  48. protected override void Invoke(object parameter)
  49. {
  50. Debug.WriteLine(Message, MessageParameter, AssociatedObject, parameter);
  51. }
  52. }
  53.  
  54. public class Behaviors : List<Behavior>
  55. {
  56. }
  57.  
  58. public class Triggers : List<TriggerBase>
  59. {
  60. }
  61.  
  62. public static class SupplementaryInteraction
  63. {
  64. public static Behaviors GetBehaviors(DependencyObject obj)
  65. {
  66. return (Behaviors)obj.GetValue(BehaviorsProperty);
  67. }
  68.  
  69. public static void SetBehaviors(DependencyObject obj, Behaviors value)
  70. {
  71. obj.SetValue(BehaviorsProperty, value);
  72. }
  73.  
  74. public static readonly DependencyProperty BehaviorsProperty =
  75. DependencyProperty.RegisterAttached("Behaviors", typeof(Behaviors), typeof(SupplementaryInteraction), new UIPropertyMetadata(null, OnPropertyBehaviorsChanged));
  76.  
  77. private static void OnPropertyBehaviorsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  78. {
  79. var behaviors = Interaction.GetBehaviors(d);
  80. foreach (var behavior in e.NewValue as Behaviors) behaviors.Add(behavior);
  81. }
  82.  
  83. public static Triggers GetTriggers(DependencyObject obj)
  84. {
  85. return (Triggers)obj.GetValue(TriggersProperty);
  86. }
  87.  
  88. public static void SetTriggers(DependencyObject obj, Triggers value)
  89. {
  90. obj.SetValue(TriggersProperty, value);
  91. }
  92.  
  93. public static readonly DependencyProperty TriggersProperty =
  94. DependencyProperty.RegisterAttached("Triggers", typeof(Triggers), typeof(SupplementaryInteraction), new UIPropertyMetadata(null, OnPropertyTriggersChanged));
  95.  
  96. private static void OnPropertyTriggersChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  97. {
  98. var triggers = Interaction.GetTriggers(d);
  99. foreach (var trigger in e.NewValue as Triggers) triggers.Add(trigger);
  100. }
  101. }
  102.  
  103. #region Attached Properties Boilerplate
  104.  
  105. public static readonly DependencyProperty IsActiveProperty = DependencyProperty.RegisterAttached("IsActive", typeof(bool), typeof(ScrollIntoViewBehavior), new PropertyMetadata(false, OnIsActiveChanged));
  106.  
  107. public static bool GetIsActive(FrameworkElement control)
  108. {
  109. return (bool)control.GetValue(IsActiveProperty);
  110. }
  111.  
  112. public static void SetIsActive(
  113. FrameworkElement control, bool value)
  114. {
  115. control.SetValue(IsActiveProperty, value);
  116. }
  117.  
  118. private static void OnIsActiveChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  119. {
  120. var behaviors = Interaction.GetBehaviors(d);
  121. var newValue = (bool)e.NewValue;
  122.  
  123. if (newValue)
  124. {
  125. //add the behavior if we don't already have one
  126. if (!behaviors.OfType<ScrollIntoViewBehavior>().Any())
  127. {
  128. behaviors.Add(new ScrollIntoViewBehavior());
  129. }
  130. }
  131. else
  132. {
  133. //remove any instance of the behavior. (There should only be one, but just in case.)
  134. foreach (var item in behaviors.ToArray())
  135. {
  136. if (item is ScrollIntoViewBehavior)
  137. behaviors.Remove(item);
  138. }
  139. }
  140. }
  141.  
  142.  
  143. #endregion
  144.  
  145. <Style TargetType="Button">
  146. <Setter Property="Blah:ScrollIntoViewBehavior.IsActive" Value="True" />
  147. </Style>
Add Comment
Please, Sign In to add comment