gpt4 book ai didi

c# - 将事件绑定(bind)到 Item ViewModel

转载 作者:太空宇宙 更新时间:2023-11-03 10:45:19 25 4
gpt4 key购买 nike

我有一个 ListView/GridView 设置,我想处理对显示项的右键单击。是否有这样做的数据绑定(bind)方式?我见过复杂的解决方法,例如 handling the super-elements event and poking around to find its origin ,但是对于这样的基本请求,感觉非常臃肿。

我希望看到的是将事件绑定(bind)到项目的 ViewModel 的操作 - 有没有办法做到这一点?类似于 this ,但我不太清楚如何调整它以处理单个 ListView 项目(我什至不确定这是否可能,老实说)。

粗略的轮廓:

<ListView>
<ListView.View>
<GridView />
</ListView.View>
<ListView.Resources>
<Style TargetType="{x:Type ListViewItem}">
</Style>
</ListView.Resources>
</ListView/>

最佳答案

有一种方法可以使用 Blend SDK 中的交互程序集。它将提供一个 EventTrigger,它会在引发事件时执行命令。

<!--
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
-->

<Button Content="Click me">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<i:InvokeCommandAction Command="{Binding ClickCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>

编辑:

您的问题的可能解决方案如下所示:

查看:

<ListView x:Name="listView">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseRightButtonUp">
<i:InvokeCommandAction Command="{Binding RightClickOnItemCommand}"
CommandParameter={Binding SelectedItem, ElementName=listView} />
</i:EventTrigger>
</i:Interaction.Triggers>
</ListView>

View 模型:

public ICommand RightClickOnItemCommand { get; set; }

public void RightClickOnItem(object item)
{
}

关于c# - 将事件绑定(bind)到 Item ViewModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23674786/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com