code_junkie

PreviewCanExecuteEvent not executed

Nov 13th, 2011
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. element.AddHandler(CommandManager.PreviewCanExecuteEvent, new CanExecuteRoutedEventHandler(scope.CanExecutedHandler), true);
  2.  
  3. <Window.Resources>
  4. <my:PermissionScope x:Key="permissionManager"/>
  5. </Window.Resources>
  6. <StackPanel>
  7. <TextBox Height="23" Name="textBox1" Width="120" />
  8. <Button Content="Permission Required" Command="{Binding Path=PermissionRequired}" my:PermissionScope.SharedPermissionScope="{StaticResource permissionManager}"/>
  9. <Button Content="Permission not required" Command="{Binding Path=PermissionRequired}"/>
  10. </StackPanel>
  11.  
  12. public class PermissionScope
  13. {
  14.  
  15. public static readonly DependencyProperty SharedPermissionScopeProperty =
  16. DependencyProperty.RegisterAttached("SharedPermissionScope", typeof(PermissionScope), typeof(PermissionScope),
  17. new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits,
  18. new PropertyChangedCallback(OnUseGlobalSharedPermissionScopeChanged)));
  19.  
  20. public static void SetSharedPermissionScope(DependencyObject depObj, PermissionScope scope)
  21. {
  22. // never place logic in here, because these methods are not called when things are done in XAML
  23. depObj.SetValue(SharedPermissionScopeProperty, scope);
  24. }
  25.  
  26. public static PermissionScope GetSharedPermissionScope(DependencyObject depObj)
  27. {
  28. // never place logic in here, because these methods are not called when things are done in XAML
  29. return depObj.GetValue(SharedPermissionScopeProperty) as PermissionScope;
  30. }
  31.  
  32. private static void OnUseGlobalSharedPermissionScopeChanged(DependencyObject depObj, DependencyPropertyChangedEventArgs args)
  33. {
  34. if (depObj is Button)
  35. {
  36. if (args.OldValue != null)
  37. {
  38. RemoveEventHandlers(depObj as UIElement, args.OldValue as PermissionScope);
  39. }
  40. if (args.NewValue != null)
  41. {
  42. AttachEventHandlers(depObj as UIElement, args.NewValue as PermissionScope);
  43. }
  44. }
  45. }
  46.  
  47. private static void AttachEventHandlers(UIElement element, PermissionScope scope)
  48. {
  49. if (element != null && scope != null)
  50. {
  51. element.AddHandler(CommandManager.PreviewCanExecuteEvent, new CanExecuteRoutedEventHandler(scope.CanExecutedHandler), true); // we need to see all events to subvert the built-in undo/redo tracking in the text boxes
  52. }
  53. }
  54.  
  55. private static void RemoveEventHandlers(UIElement element, PermissionScope scope)
  56. {
  57. if (element != null && scope != null)
  58. {
  59. element.AddHandler(CommandManager.PreviewCanExecuteEvent, new CanExecuteRoutedEventHandler(scope.CanExecutedHandler));
  60. }
  61. }
  62.  
  63. private void CanExecutedHandler(object sender, CanExecuteRoutedEventArgs e)
  64. {
  65. if (e.Command is CommandBase)
  66. {
  67. bool hasPermission = false;
  68. hasPermission = ((CommandBase)e.Command).HasPermission();
  69.  
  70. ShowControl((UIElement)e.OriginalSource, hasPermission);
  71. }
  72. }
  73.  
  74. public static void ShowControl(UIElement element, bool show)
  75. {
  76. element.Visibility = show ? Visibility.Visible : Visibility.Collapsed;
  77. }
  78. }
Add Comment
Please, Sign In to add comment