4

我正在尝试使用 MVVM 模式绑定 LiveChart 的笛卡尔图表元素的“DataClick”事件。

我有这样的 Charts.xml:

<ContentControl Grid.Row="0">
        <lvc:CartesianChart x:Name="ContrastChart" Series="{Binding ContrastSeriesCollection}">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="DataClick">
                    <i:InvokeCommandAction Command="{Binding ChartDataClick}" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </lvc:CartesianChart>
    </ContentControl>

这是我的 ViewModel 上的 ICommand ChartDataClick:

        public ICommand ChartDataClick {
        get
        {
            if(_dataClickCommand == null)
            {
                _dataClickCommand = new DelegateCommand( 
                    () => 
                    {
                        MessageBox.Show("Data Clicked!");
                    } 
                    );
            }

            return _dataClickCommand;
        }
    }

如果我将例如“DataClick”切换为“MouseEnter”,我会触发我的命令。

所以我假设问题在于 DataClick 是一个自定义事件。

有人知道解决方法吗?我真的尝试了所有可以在 Google 上找到的可以提供帮助的方法,但到目前为止还没有...

LiveCharts 事件:事件文档

4

1 回答 1

2

EventTrigger不歧视。

我们可以通过实现具有自定义 Routed Event 的MyButtonSimpleTap来检查这一点。

我们可以从后面的代码中的处理程序

<custom:MyButtonSimple 
    x:Name="mybtnsimple" Tap="mybtnsimple_Tap"
    Content="Click to see Tap custom event work">
</custom:MyButtonSimple>

到 ViewModel ICommand

<custom:MyButtonSimple 
    x:Name="mybtnsimple"  
    Content="Click to see Tap custom event work">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Tap">
            <i:InvokeCommandAction Command="{Binding Command}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</custom:MyButtonSimple>

一切都按预期工作

这些触发器的缺点是它们必须放置在引发事件的UIElement上。换句话说,他们忽略冒泡或隧道事件。这就是为什么没有Interaction.Triggers其他选择的原因:

<Grid custom:MyButtonSimple.Tap="mybtnsimple_Tap">
    <custom:MyButtonSimple 
        x:Name="mybtnsimple"  
        Content="Click to see Tap custom event work">
    </custom:MyButtonSimple>
</Grid>

总而言之,DataClick事件不会在CartesianChart(但在逻辑树的下方)引发,因此您不能以这种方式处理它。

于 2016-10-11T16:19:12.060 回答