Wpf 是否可以将CallMethodAction交互与参数一起使用?

Wpf 是否可以将CallMethodAction交互与参数一起使用?,wpf,xaml,blend,Wpf,Xaml,Blend,鉴于以下情况: <Storyboard x:Key="Foo" AutoReverse="True" RepeatBehavior="3x"> <Storyboard.Children/> </Storyboard> <DoubleAnimationUsingKeyFrames x:Key = "Bar"/> <ei:DataTrigger Binding="{ Binding SomeVar,

鉴于以下情况:

<Storyboard x:Key="Foo" AutoReverse="True" RepeatBehavior="3x">
    <Storyboard.Children/>
</Storyboard>

<DoubleAnimationUsingKeyFrames x:Key = "Bar"/>

<ei:DataTrigger
    Binding="{
        Binding SomeVar,
        ElementName=SomeElement,
        FallbackValue=False,
        Mode=OneWay}"
    Value="True">
    <ei:CallMethodAction
        TargetObject="{
            Binding Mode=OneWay,
            Path=Children,
            Source={StaticResource Foo}}"
        MethodName="Add"/>
</ei:DataTrigger>


是否有任何方法可以将
Bar
作为参数传递给方法调用
子项。添加

您可以尝试从交互中使用InvokeCommandAction

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

<ei:DataTrigger
        Binding="{
        Binding SomeVar,
        ElementName=SomeElement,
        FallbackValue=False,
        Mode=OneWay}"
    Value="True">
    <i:InvokeCommandAction Command="{Binding SomeCommand, Source={StaticResource SomeViewModel}}" CommandParameter="Bar"/>
</ei:DataTrigger>
xmlns:i=”http://schemas.microsoft.com/expression/2010/interactivity"

CallMethodAction
只能用于调用不带参数的方法或具有两个参数的方法,其中第一个参数的类型为object,第二个参数的类型为EventArgs

有鉴于此,您将无法使用
CallMethodAction
执行您想要的操作。但是,您可以创建自己的触发器操作,该操作将调用您的方法并传入您指定的值。我只做了一些轻微的测试,但它应该非常接近你需要的

使用系统;
使用System.Collections.Generic;
使用System.Linq;
运用系统反思;
使用System.Windows;
使用System.Windows.Interactive;
命名空间本地操作
{
公共类CallUnaryMethodAction:TargetedTriggerAction
{
//要调用的方法的名称。
公共静态只读DependencyProperty MethodNameProperty=
DependencyProperty.Register(“MethodName”,
类型(字符串),
类型(Callunarymethodation),
新属性元数据(已更新的ONNEDSMETHOD);
公共字符串方法名
{
获取{return(string)GetValue(MethodNameProperty);}
set{SetValue(MethodNameProperty,value);}
}
//标志,用于确定是否要在目标对象中搜索非公共方法。
公共静态只读从属属性AllowOnPublicMethodsProperty=
DependencyProperty.Register(“AllowOnPublicMethods”,
类型(bool),
类型(Callunarymethodation),
新属性元数据(已更新的ONNEDSMETHOD);
公共bool AllowNonPublicMethods
{
获取{return(bool)GetValue(AllowNonPublicMethodsProperty);}
set{SetValue(AllowNonPublicMethodsProperty,value);}
}
//要传递给方法的参数。如果未设置此参数,则传递的值
//将改为使用触发器操作的Invoke方法。
公共静态只读从属属性ParameterProperty=
DependencyProperty.Register(“参数”,
类型(对象),
类型(Callunarymethodation));
公共对象参数
{
获取{返回GetValue(ParameterProperty);}
set{SetValue(参数属性,值);}
}
私有静态void onneedsmethodinfounded更新(DependencyObject d、DependencyPropertyChangedEventArgs e)
{
var action=d为Callunarymethodation;
如果(操作!=null)
action.UpdateMethodInfo();
}
受保护的覆盖无效附加()
{
UpdateMethodInfo();
}
受保护的覆盖无效OnTargetChanged(DependencyObject oldTarget、DependencyObject newTarget)
{
UpdateMethodInfo();
}
受保护的覆盖无效调用(对象参数)
{
对象目标=this.TargetObject??this.associated对象;
if(target==null)
返回;
//确定我们将传递给方法的内容。
object methodParam=ReadLocalValue(ParameterProperty)==DependencyProperty.UnsetValue?
参数:此参数;
//根据要传递的参数,选择要调用的最佳方法。
MethodMethodToCall=m_methods.FirstOrDefault(方法=>
(methodParam!=null)&&method.ParameterInfo.ParameterType.IsAssignableFrom(methodParam.GetType());
if(methodToCall==null)
抛出新的InvalidOperationException(“未找到合适的方法”);
methodToCall.MethodInfo.Invoke(目标,新对象[]{methodParam});
}
私有void UpdateMethodInfo()
{
m_方法。清除();
对象目标=this.TargetObject??this.associated对象;
if(target==null | | string.IsNullOrEmpty(this.MethodName))
返回;
//查找具有给定名称的所有一元方法。
BindingFlags=BindingFlags.Public | BindingFlags.Instance;
if(this.AllowNonPublicMethods)
flags |=BindingFlags.NonPublic;
foreach(target.GetType().GetMethods(标志)中的MethodInfo MethodInfo)
{
if(methodInfo.Name==this.MethodName)
{
ParameterInfo[]parameters=methodInfo.GetParameters();
if(parameters.Length==1)
m_methods.Add(新方法(methodInfo,参数[0]);
}
}
//对方法进行排序,以便首先对具有大多数派生参数的方法进行排序。
//这将帮助我们在调用中选择最合适的方法进行调用。
m_methods=m_methods.OrderByDescending(方法=>
{
int秩=0;
for(Type Type=method.ParameterInfo.ParameterType;Type!=typeof(对象);Type=Type.BaseType)
++等级;
返回等级;
})。ToList();
}
私有列表m_方法=新列表();
//保存我们可以调用的可能方法列表的信息。
私有类方法
{
公共方法(MethodInfo MethodInfo、ParameterInfo paramInfo)
{
this.MethodInfo=MethodInfo;
this.ParameterInfo=paramI
...
xmlns:local="clr-namespace:LocalActions"
...

<ei:DataTrigger
    Binding="{
        Binding SomeVar,
        ElementName=SomeElement,
        FallbackValue=False,
        Mode=OneWay}"
    Value="True">
    <local:CallUnaryMethodAction
        TargetObject="{
            Binding Mode=OneWay,
            Path=Children,
            Source={StaticResource Foo}}"
        MethodName="Add"
        Parameter="{StaticResource Bar}"/>
</ei:DataTrigger>