2

Me and my team are working on a WPF project which makes use of SetPropertyAction similar to what can be found right below:

Setting a property with an EventTrigger

The only difference is that since we're using .NET 4.0, there's no propertyInfo.SetValue (at the last line) which takes 2 arguments: .NET 4.0 version actually needs at least 3 arguments. Here's the modified version which we're using (only the last few lines are different):

using System.Reflection;
using System.Windows;
using System.Windows.Interactivity;
using System.Windows.Controls;

namespace TRIM.Scada.UI
{

        /// <summary>
        /// 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.
        /// </summary>
        public class SetPropertyAction : TriggerAction<FrameworkElement>
        {
            // PropertyName DependencyProperty.

            /// <summary>
            /// The property to be executed in response to the trigger.
            /// </summary>
            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.

            /// <summary>
            /// The value to set the property to.
            /// </summary>
            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.

            /// <summary>
            /// Specifies the object upon which to set the property.
            /// </summary>
            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);

                object value;

                if (propertyInfo.PropertyType == typeof(bool))
                {
                    if (PropertyValue.ToString().ToLower() == "true")
                        value = true;
                    else
                        value = false;
                }
                else
                {
                    value = PropertyValue;
                }


                propertyInfo.SetValue(target, value, null);
            }
        }

}

We need that in order to be able to set a UserControl property directly from XAML through an eventTrigger, with code like this:

<i:Interaction.Triggers>
    <i:EventTrigger EventName="MouseEnter">
        <my:SetPropertyAction PropertyName="Tag" PropertyValue="test" TargetObject="{Binding ACanvas}"></my:SetPropertyAction>
    </i:EventTrigger>
</i:Interaction.Triggers>

Where "i" prefix refers to System.Windows.Interactivity.dll which is referenced in our project (v4.0.30319).

All this nicely compiles (and works) on all our PCs, but in one of them (which also has Blend installed) Visual Studio 2012 reports this error in the XAML Designer (while it still compiles and works without issues) underlining "my:SetPropertyAction":

"Cannot add instance of SetPropertyAction to a collection of type TriggerActionCollection. Only items of T are allowed"

How come this is happening while the program still compiles and still works on ALL our pc? Is that maybe a problem with visual studio? Can we ignore that? Thanks.

Community
  • 1
  • 1
Carlo Arnaboldi
  • 363
  • 2
  • 15

1 Answers1

0

Add reference to proper 4.5 versions. You might be using 4.0 versions. When you add reference in your Project explorer, on my machine I can see 3 System.Windows.Interactivity dlls with 4.5, 4.0, and 3.5 versions. You can check for 4.5 version.

Or you can make sure that you are using right version out of available ones.

Hope this helps.

AnjumSKhan
  • 9,647
  • 1
  • 26
  • 38