与自定义事件触发器一起使用时,不会触发MouseLeftButton事件

时间:2016-04-07 05:37:45

标签: c# wpf events triggers

使用了如下所示的事件触发器,

<i:Interaction.Triggers>
        <local:EventTriggerExt EventName="MouseLeftButtonDown">
            <i:InvokeCommandAction Command="{Binding BuyItemCommand}" />
        </local:EventTriggerExt>
        <local:EventTriggerExt EventName="MouseRightButtonDown">
            <i:InvokeCommandAction Command="{Binding SellItemCommand}" />
        </local:EventTriggerExt>
    </i:Interaction.Triggers>

代码背后:

class EventTriggerExt :System.Windows.Interactivity.EventTrigger
{
    protected override void OnEvent(EventArgs eventArgs)
    {
        if(this.AssociatedObject is SfDataGrid && eventArgs is MouseButtonEventArgs)
        {
            var args = eventArgs as MouseButtonEventArgs;

        }
        base.OnEvent(eventArgs);
    }
}

当我点击DataGrid Cell时,MouseRightButtonDown事件与SellItemCommand配合得很好。但MouseLeftButtonDown事件不起作用,也适用于LeftClick事件。希望任何人都面对这一点,并提出相同的解决方案。

有什么想法吗?

此致 Smirti

1 个答案:

答案 0 :(得分:0)

如果您点击MouseLeftButtonDown单元格,则不会触发

DataGrid事件。这是因为SelectionChanged事件将在MouseLeftButtonDown之前被调用,之后它将不会使事件冒泡。如果单击DataGrid的其他部分而不是单元格,例如在最后一列之后的空白区域,您会注意到正在调用MouseLeftButtonDown

而不是&#39; MouseLeftButtonDown&#39;您可以使用PreviewMouseLeftButtonDown之前调用的SelectionChanged事件。