using System.ComponentModel; using System.Windows; namespace OpenSEMI.ClientBase.Command { public class PropertyCommandTrigger : CommandTrigger { public static readonly DependencyProperty PropertyProperty = DependencyProperty.Register("Property", typeof(DependencyProperty), typeof(PropertyCommandTrigger), new FrameworkPropertyMetadata(null)); public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(string), typeof(PropertyCommandTrigger), new FrameworkPropertyMetadata(null)); public static readonly DependencyProperty TProperty = DependencyProperty.Register("T", typeof(object), typeof(PropertyCommandTrigger), new FrameworkPropertyMetadata(null, OnTChanged)); public DependencyProperty Property { get { return (DependencyProperty)GetValue(PropertyProperty); } set { SetValue(PropertyProperty, value); } } public string Value { get { return (string)GetValue(ValueProperty); } set { SetValue(ValueProperty, value); } } public object T { get { return GetValue(TProperty); } set { SetValue(TProperty, value); } } protected override Freezable CreateInstanceCore() { return new PropertyCommandTrigger(); } private static void OnTChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { } protected override void InitializeCore(FrameworkElement source) { DependencyPropertyDescriptor descriptor = DependencyPropertyDescriptor.FromProperty(Property, source.GetType()); descriptor.AddValueChanged(source, delegate { CommandParameter parameter = new PropertyCommandParameter(base.CustomParameter, Property, source.GetValue(Property)); object objB = Value; if (descriptor.Converter.CanConvertFrom(typeof(string))) { objB = descriptor.Converter.ConvertFromString(Value); } if (object.Equals(source.GetValue(Property), objB)) { ExecuteCommand(parameter); } }); } } }