以编程方式为InvokeCommandAction设置绑定

时间:2012-09-04 06:58:55

标签: c# silverlight

我无法以编程方式设置此绑定:

<i:Interaction.Triggers>
    <i:EventTrigger EventName="MouseLeftButtonDown">
        <i:InvokeCommandAction Command="{Binding Path=Document.MyDelegateCommandProperty}" />
    </i:EventTrigger>
</i:Interaction.Triggers>

我尝试过:

InvokeCommandAction ica = new InvokeCommandAction();
Binding actionCommandBinding = new Binding("Document.MyDelegateCommandProperty");
BindingOperations.SetBinding(ica, InvokeCommandAction.CommandProperty, actionCommandBinding);
System.Windows.Interactivity.EventTrigger eventTrigger = new System.Windows.Interactivity.EventTrigger("MouseLeftButtonDown");
eventTrigger.Actions.Add(ica);
eventTrigger.Attach(myUiElement);

任何人都可以提供帮助吗?

3 个答案:

答案 0 :(得分:2)

这似乎是一个老帖子,但由于我遇到了同样的问题,我想我会发布解决方案。

替换 eventTrigger.Attach(myUiElement);Interaction.GetTriggers(myUIElement).Add(eventTrigger);

答案 1 :(得分:0)

从未在代码中使用Binding,但根据Binding类的MSDN示例,您必须设置Binding.Source属性

Binding actionCommandBinding = new Binding("Document.MyDelegateCommandProperty");
actionCommandBinding.Source = ......

你只是在没有源对象/ Datacontext的构造函数中传递string

这篇文章对你有帮助吗?

答案 2 :(得分:0)

试试这个。

Sub MapEventToCommand(eventNameSource As String, commandDestination As string)
        Dim t = New InvokeCommandAction()
        Dim commandAction As New InvokeCommandAction()
        Dim actionCommandBinding As New Binding(commandDestination)
        BindingOperations.SetBinding(commandAction, InvokeCommandAction.CommandProperty, actionCommandBinding)
        Dim eventTrigger As New EventTrigger(eventNameSource)
        eventTrigger.Actions.Add(commandAction)
        Interaction.GetTriggers(Me).Add(eventTrigger)
    End Sub