1

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

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

任何人都可以帮忙吗?

4

3 回答 3

2

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

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

于 2013-04-11T21:02:34.330 回答
0

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

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

你只是传入string没有源对象/数据上下文的构造函数。

这篇文章对你有帮助吗?

于 2012-09-06T07:34:40.210 回答
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
于 2014-12-19T17:33:55.470 回答