WPF 附加属性的用法 (一)

 

public class MDCTest
    {
        public static DependencyProperty MouseDoubleClickCommandProperty = DependencyProperty.RegisterAttached(
            "MouseDoubleClick",
            typeof(ICommand),
            typeof(MDCTest),
            new FrameworkPropertyMetadata(null, new PropertyChangedCallback(MouseDoubleClickChanged))
            );
        public static void SetMouseDoubleClick(DependencyObject target, ICommand value)
        {
            target.SetValue(MDCTest.MouseDoubleClickCommandProperty, value);
        }
        public static ICommand GetMouseDoubleClick(DependencyObject target)
        {
            return (ICommand)target.GetValue(MDCTest.MouseDoubleClickCommandProperty);
        }
        private static void MouseDoubleClickChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
        {
            Control control = target as Control;
            if (control != null)
            {
                if (e.NewValue != null && e.OldValue == null)
                {
                    control.MouseDoubleClick += new MouseButtonEventHandler(control_MouseDoubleClick);
                }
                else if (e.NewValue == null && e.OldValue != null)
                {
                    control.MouseDoubleClick -= new MouseButtonEventHandler(control_MouseDoubleClick);
                }
            }
        }
        public static void control_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            Control control = sender as Control;
            ICommand command = (ICommand)control.GetValue(MDCTest.MouseDoubleClickCommandProperty);
            command.Execute(control);
        }
    }
复制代码
复制代码
public class RelayCommand : ICommand
    {
        private Action _Execute;
        private Predicate _CanExecute;

        public RelayCommand(Action execte)
            : this(execte, null)
        {
        }
        public RelayCommand(Action execute, Predicate canExecute)
        {
            if (execute == null)
                throw new ArgumentNullException("Execute");
            _Execute = execute;
            _CanExecute = canExecute;
        }
        public bool CanExecute(object parameter)
        {
            return _CanExecute == null ? true : _CanExecute(parameter);
        }

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        public void Execute(object parameter)
        {
            _Execute(parameter);
        }
    } 
    
复制代码
复制代码
public class LabelViewModel
    {
        public ICommand Command
        {
            get
            {
                return new RelayCommand((m) => MessageBox.Show(m.ToString() + "command双击事件成功"));
            }
        }
    }
复制代码
     

给Label附加双击事件 原文:http://www.cnblogs.com/ptfblog/archive/2011/07/11/2103183.html

 

绑定有两个需要注意的地方

1.如果绑定到 附加属性(Binding Attached Property),需要加上括号,这个比较特别,例如

 

复制代码
复制代码
   
复制代码
复制代码
复制代码
复制代码

          
            
            
          
        
复制代码
复制代码

2.如果绑定到只读的属性(Binding to readonly property),例如IsFocused,需要加上 Mode = OneWay

复制代码
复制代码

          
            
            
          
        
复制代码
 
https://www.cnblogs.com/gaobw/p/6553443.html

转载于:https://www.cnblogs.com/sjqq/p/8456994.html

你可能感兴趣的:(WPF 附加属性的用法 (一))