[英]WPF, MVVM, EventBehaviourFactory in ListView, binding event to command
i am basically trying to follow http://blog.functionalfun.net/2008/09/hooking-up-commands-to-events-in-wpf.html strategy, to bind an event to command. 我基本上是尝试遵循http://blog.functionalfun.net/2008/09/hooking-up-commands-to-events-in-wpf.html策略,将事件绑定到命令。
i have a ListView, where i would like to start an instance of ICommand on double click on a row. 我有一个ListView,我想在其中双击一行来启动ICommand的实例。 a ListView or ListBoxItem on it do not have a Command Property.
ListView或ListBoxItem上面没有Command属性。 this problem (so it seems) was solved by using attached property, but i personally still cannot figure out how to use it on ListView.
这个问题(看来)是通过使用附加属性解决的,但是我个人仍然无法弄清楚如何在ListView上使用它。
I have a collection of "Signals", each has a property "Name". 我有一个“信号”集合,每个都有一个“名称”属性。 Below is a part of my xaml.
以下是我的xaml的一部分。
<ListView ItemsSource="{Binding Signals}" >
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="{models:ListBoxItemBehaviour.DoubleClickCommand}" Value="{Binding Command1}" />
</Style>
</ListBox.ItemContainerStyle>
<ListView.View>
<GridView>
<GridViewColumn Header="Signal" DisplayMemberBinding="{Binding Name}" />
</GridView>
</ListView.View>
</ListView>
this does not work. 这行不通。 ListBoxItemBehaviour is an implementation of a behaviour, following link above, hooked on ListBoxItem.MouseDoubleClickEvent event.
ListBoxItemBehaviour是行为的实现,通过上面的链接,它钩在ListBoxItem.MouseDoubleClickEvent事件上。
i suspect the error above is small, but not obvious to me. 我怀疑上面的错误很小,但对我来说并不明显。 any suggestions?
有什么建议么?
first error is: "Nested types are not supported:".. 第一个错误是:“不支持嵌套类型:”。
In a ListView
you can use the InputBindings to get the Mouse- and Keyboard-Inputs on it. 在
ListView
,可以使用InputBindings在其上获取鼠标和键盘输入。
To bind the MouseDoubleClick to a Command you can use 要将MouseDoubleClick绑定到可以使用的命令
<ListView.InputBindings>
<MouseBinding MouseAction="LeftDoubleClick" Command="{Binding ListViewDoubleClickCommand}"/>
</ListView.InputBindings>
well, since i was not able to find a proper solution, but work is work, i sacrificed purity of MVVM pattern over workability. 好吧,因为我无法找到合适的解决方案,但是工作就是工作,所以我牺牲了MVVM模式的纯度而不是可操作性。
added this event to xaml 将此事件添加到xaml
<EventSetter Event="MouseDoubleClick" Handler="HandleDoubleClick" />
and handler to code behind 和处理程序在后面编码
protected void HandleDoubleClick(object sender, MouseButtonEventArgs e)
{
MyViewModel mvm = this.DataContext as MyViewModel;
mvm.MyCommand.Execute();
}
thus double click on item in the viewlist will trigger the command. 因此,双击视图列表中的项目将触发命令。 not beautiful, but works.
不漂亮,但可以。 if i find the proper pure MVVM solution, i will post it.
如果我找到合适的纯MVVM解决方案,我将发布它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.