Ren*_*ira 5 c# wpf prism mvvm livecharts
我正在尝试使用 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>
Run Code Online (Sandbox Code Playgroud)
这是我的 ViewModel 上的 ICommand ChartDataClick:
public ICommand ChartDataClick {
get
{
if(_dataClickCommand == null)
{
_dataClickCommand = new DelegateCommand(
() =>
{
MessageBox.Show("Data Clicked!");
}
);
}
return _dataClickCommand;
}
}
Run Code Online (Sandbox Code Playgroud)
如果我将“DataClick”切换为“MouseEnter”,我的命令就会被触发。
所以我假设问题是 DataClick 是自定义事件。
有人知道这个问题的解决方法吗?我真的尝试了在谷歌上能找到的所有有帮助的东西,但到目前为止什么也没有......
LiveCharts 事件:事件文档
对方EventTrigger不歧视。
我们可以通过实现具有自定义路由事件的MyButtonSimpleTap来检查这一点。
我们可以从后面的代码中的处理程序开始
<custom:MyButtonSimple
x:Name="mybtnsimple" Tap="mybtnsimple_Tap"
Content="Click to see Tap custom event work">
</custom:MyButtonSimple>
Run Code Online (Sandbox Code Playgroud)
至 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>
Run Code Online (Sandbox Code Playgroud)
一切都按预期进行
这些触发器的缺点是它们必须放置在引发事件的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>
Run Code Online (Sandbox Code Playgroud)
总而言之,该DataClick事件不是在逻辑树上引发的CartesianChart(而是在逻辑树的更下方),因此您不能以这种方式处理它。