XAML
<!--鼠标左键双击-->
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Text}" ToolTip="鼠标左键双击,采集本年度!">
<TextBlock.InputBindings>
<MouseBinding Command="{Binding DataContext.SelectListBoxItemCommand, ElementName=mainWin }"
CommandParameter="{Binding ElementName=lst}"
MouseAction="LeftDoubleClick"></MouseBinding>
</TextBlock.InputBindings>
</TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
DataContext.SelectListBoxItemCommand 绑定的命令
ElementName=mainWin 这里是当前窗口名称,也就是DataContext的宿主
CommandParameter="{Binding ElementName=lst}" lst为ListBox名称
MouseAction="LeftDoubleClick" Action类型
Model
public class TextItem
{
public TextItem(string text)
{
Text = text;
}
public string Text { get; set; }
}
ViewModel
private ICommand _SelectListBoxItemCommand;
public ICommand SelectListBoxItemCommand
{
get
{
if (this._SelectListBoxItemCommand == null)
this._SelectListBoxItemCommand = new RelayCommandBase(SelectList_MouseDoubleClick);
return this._SelectListBoxItemCommand;
}
}
private void SelectList_MouseDoubleClick(object obj)
{
var lst= obj as ListBox;
TextItem textItem = lst.SelectedItem as TextItem;
//Task.Run(()=> GetOneYear(textItem));//实现
}
obj可以接收 CommandParameter 传递的参数。即Listbox