Setting a property with an EventTrigger

前端 未结 4 2063
你的背包
你的背包 2020-11-27 02:35

I want to be able to set a property with an EventTrigger, there\'s a number of problems with this.

1) EventTriggers only support Actions, so I must use a storyBoard

4条回答
  •  [愿得一人]
    2020-11-27 03:35

    Just create your own action.

    namespace WpfUtil
    {
        using System.Reflection;
        using System.Windows;
        using System.Windows.Interactivity;
    
    
        /// 
        /// Sets the designated property to the supplied value. TargetObject
        /// optionally designates the object on which to set the property. If
        /// TargetObject is not supplied then the property is set on the object
        /// to which the trigger is attached.
        /// 
        public class SetPropertyAction : TriggerAction
        {
            // PropertyName DependencyProperty.
    
            /// 
            /// The property to be executed in response to the trigger.
            /// 
            public string PropertyName
            {
                get { return (string)GetValue(PropertyNameProperty); }
                set { SetValue(PropertyNameProperty, value); }
            }
    
            public static readonly DependencyProperty PropertyNameProperty
                = DependencyProperty.Register("PropertyName", typeof(string),
                typeof(SetPropertyAction));
    
    
            // PropertyValue DependencyProperty.
    
            /// 
            /// The value to set the property to.
            /// 
            public object PropertyValue
            {
                get { return GetValue(PropertyValueProperty); }
                set { SetValue(PropertyValueProperty, value); }
            }
    
            public static readonly DependencyProperty PropertyValueProperty
                = DependencyProperty.Register("PropertyValue", typeof(object),
                typeof(SetPropertyAction));
    
    
            // TargetObject DependencyProperty.
    
            /// 
            /// Specifies the object upon which to set the property.
            /// 
            public object TargetObject
            {
                get { return GetValue(TargetObjectProperty); }
                set { SetValue(TargetObjectProperty, value); }
            }
    
            public static readonly DependencyProperty TargetObjectProperty
                = DependencyProperty.Register("TargetObject", typeof(object),
                typeof(SetPropertyAction));
    
    
            // Private Implementation.
    
            protected override void Invoke(object parameter)
            {
                object target = TargetObject ?? AssociatedObject;
                PropertyInfo propertyInfo = target.GetType().GetProperty(
                    PropertyName,
                    BindingFlags.Instance|BindingFlags.Public
                    |BindingFlags.NonPublic|BindingFlags.InvokeMethod);
    
                propertyInfo.SetValue(target, PropertyValue);
            }
        }
    }
    

    In this case I'm binding to a property called DialogResult on my viewmodel.

    
    
        
    
    
    

提交回复
热议问题