【发布时间】:2019-10-22 11:44:41
【问题描述】:
我需要在我的ListView 的一个项目点击 时触发command。 Event 存在于 ViewModel 中,不应在后面的代码中实现。
我目前正在使用EventSetter 处理它,命令在后面的代码中,但我需要它在ViewModel 中。
简而言之,只要单击列表中的一项,我就想更改 property1 和 property2(存在于 ViewModel 中)的值。
请帮忙?
XAML:
<ListView.ItemContainerStyle>
<Style TargetType= "ListViewItem">
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="MyCommand"/>
</Style>
</ListView.ItemContainerStyle>
代码隐藏:
private voide MyCommand(object sender, MouseButtonEventArgs e)
{
ViewModel.property1 = true;
ViewModel.property2 = false;
}
【问题讨论】:
-
看看现有的thread,别忘了创建你的虚拟机
-
只是澄清一下,该命令不应该实现项目的选择?
-
正如 Clemens 所写...你为什么不使用 ListView 的 SelectedItem 呢?
-
@Clemens 我需要在单击项目后立即设置某些属性的值,所以我正在使用命令来执行此操作。
-
@Selvin 我正在尝试但失败了。你能举个例子解释一下吗?