C#/ WPF .NET 4.0 - 无法将SetPropertyAction的实例添加到TriggerActionCollection类型的集合中。只允许T项

时间:2015-02-18 14:50:53

标签: c# wpf .net-4.0

我和我的团队正在开发一个WPF项目,它使用类似于下面的SetPropertyAction:

Setting a property with an EventTrigger

唯一的区别是,由于我们使用.NET 4.0,因此没有propertyInfo.SetValue(在最后一行),它有两个参数:.NET 4.0版本实际上至少需要3个参数。这是我们正在使用的修改版本(只有最后几行不同):

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);
            }
        }

}

我们需要这样才能通过eventTrigger直接从XAML设置UserControl属性,代码如下:

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

哪里&#34;我&#34; prefix指的是我们项目中引用的System.Windows.Interactivity.dll(v4.0.30319)。

所有这些都很好地编译(并且可以)在我们所有的PC上,但在其中一个(也安装了Blend)Visual Studio 2012在XAML设计器中报告此错误(虽然它仍然编译并且无问题地工作)下划线& #34;我:SetPropertyAction&#34;:

&#34;无法将SetPropertyAction的实例添加到TriggerActionCollection类型的集合中。只允许使用T项&#34;

如果程序仍在编译并且仍可在我们的所有电脑上运行,那怎么会发生这种情况?这可能是视觉工作室的问题吗?我们能忽视吗?感谢。

0 个答案:

没有答案