Advertisement
Guest User

IsEnabled and CanExecute

a guest
Jul 20th, 2012
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.75 KB | None | 0 0
  1. using System;
  2. using System.Windows;
  3. using System.Windows.Input;
  4. using Microsoft.Phone.Shell;
  5. using System.Linq;
  6.  
  7. namespace Phone7.Fx.Controls
  8. {
  9.     public class BindableApplicationBarIconButton : FrameworkElement, IApplicationBarIconButton
  10.     {
  11.         public int Index { get; set; }
  12.  
  13.         public static DependencyProperty CommandProperty = DependencyProperty.RegisterAttached("Command", typeof(ICommand), typeof(BindableApplicationBarIconButton), new PropertyMetadata(null, OnCommandPropertyChanged));
  14.         private static void OnCommandPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  15.         {
  16.             if (e.NewValue != e.OldValue)
  17.             {
  18.                 ((BindableApplicationBarIconButton)d).Command = (ICommand)e.NewValue;
  19.             }
  20.         }
  21.  
  22.         public ICommand Command
  23.         {
  24.             get { return (ICommand)GetValue(CommandProperty); }
  25.             set {
  26.                     Command.CanExecuteChanged -= ValueOnCanExecuteChanged;
  27.                 SetValue(CommandProperty, value);
  28.                 Command.CanExecuteChanged += ValueOnCanExecuteChanged;
  29.             }
  30.         }
  31.  
  32.         private void ValueOnCanExecuteChanged(object sender, EventArgs eventArgs)
  33.         {
  34.             ICommand commandSender = sender as ICommand;
  35.             if(commandSender != null)
  36.             {IsEnabled = commandSender.CanExecute(null);}
  37.         }
  38.  
  39.         public static readonly DependencyProperty CommandParameterProperty =
  40.             DependencyProperty.RegisterAttached("CommandParameter", typeof(object), typeof(BindableApplicationBarIconButton), new PropertyMetadata(null, OnCommandParameterPropertyChanged));
  41.         private static void OnCommandParameterPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  42.         {
  43.             var invokeCommand = d as BindableApplicationBarIconButton;
  44.             if (invokeCommand != null)
  45.             {
  46.                 invokeCommand.SetValue(CommandParameterProperty, e.NewValue);
  47.             }
  48.         }
  49.         public object CommandParameter
  50.         {
  51.             get { return GetValue(CommandParameterProperty); }
  52.             set
  53.             {
  54.                 SetValue(CommandParameterProperty, value);
  55.             }
  56.         }
  57.  
  58.  
  59.         public static readonly DependencyProperty CommandParameterValueProperty =
  60.           DependencyProperty.RegisterAttached("CommandParameterValue", typeof(object), typeof(BindableApplicationBarIconButton), null);
  61.         public object CommandParameterValue
  62.         {
  63.             get
  64.             {
  65.                 var returnValue = GetValue(CommandParameterValueProperty);
  66.                 return returnValue;
  67.             }
  68.             set { SetValue(CommandParameterValueProperty, value); }
  69.         }
  70.  
  71.         public static readonly DependencyProperty IsEnabledProperty =
  72.             DependencyProperty.RegisterAttached("IsEnabled", typeof(bool), typeof(BindableApplicationBarIconButton), new PropertyMetadata(true, OnEnabledChanged));
  73.  
  74.         private static void OnEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  75.         {
  76.             if (e.NewValue != e.OldValue)
  77.             {
  78.                 ((BindableApplicationBarIconButton)d).Button.IsEnabled = (bool)e.NewValue;
  79.             }
  80.         }
  81.  
  82.         public static readonly DependencyProperty TextProperty =
  83.             DependencyProperty.RegisterAttached("Text", typeof(string), typeof(BindableApplicationBarIconButton), new PropertyMetadata(OnTextChanged));
  84.  
  85.         public new static readonly DependencyProperty VisibilityProperty =
  86.            DependencyProperty.RegisterAttached("Visibility", typeof(Visibility), typeof(BindableApplicationBarIconButton), new PropertyMetadata(OnVisibilityChanged));
  87.  
  88.         private static void OnVisibilityChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  89.         {
  90.             if (e.NewValue != e.OldValue)
  91.             {
  92.                 var button = ((BindableApplicationBarIconButton)d);
  93.                 BindableApplicationBar bar = button.Parent as BindableApplicationBar;
  94.  
  95.                 bar.Invalidate();
  96.             }
  97.         }
  98.  
  99.         public new Visibility Visibility
  100.         {
  101.             get { return (Visibility)GetValue(VisibilityProperty); }
  102.             set { SetValue(VisibilityProperty, value); }
  103.         }
  104.  
  105.         private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  106.         {
  107.             if (e.NewValue != e.OldValue)
  108.             {
  109.                 ((BindableApplicationBarIconButton)d).Button.Text = e.NewValue.ToString();
  110.             }
  111.         }
  112.  
  113.         public ApplicationBarIconButton Button { get; set; }
  114.  
  115.         public BindableApplicationBarIconButton()
  116.         {
  117.             Button = new ApplicationBarIconButton();
  118.             Button.Text = "Text";
  119.             Button.Click += ApplicationBarIconButtonClick;
  120.         }
  121.  
  122.         void ApplicationBarIconButtonClick(object sender, EventArgs e)
  123.         {
  124.             if (Command != null && CommandParameter != null)
  125.                 Command.Execute(CommandParameter);
  126.             else if (Command != null)
  127.                 Command.Execute(CommandParameterValue);
  128.             if (Click != null)
  129.                 Click(this, e);
  130.         }
  131.  
  132.         public bool IsEnabled
  133.         {
  134.             get { return (bool)GetValue(IsEnabledProperty); }
  135.             set { SetValue(IsEnabledProperty, value); }
  136.         }
  137.  
  138.         public string Text
  139.         {
  140.             get { return (string)GetValue(TextProperty); }
  141.             set { SetValue(TextProperty, value); }
  142.         }
  143.  
  144.         public event EventHandler Click;
  145.  
  146.         public Uri IconUri
  147.         {
  148.             get { return Button.IconUri; }
  149.             set { Button.IconUri = value; }
  150.         }
  151.     }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement