我想要的:如果我在ListViewItem上做一个左双击,我想调用一个方法。
What I have:
Xaml:
<ListView ScrollViewer.HorizontalScrollBarVisibility="Hidden"
Name="StructListView"
Margin="15"
Grid.Row="2"
BorderThickness="0"
SelectedItem="{Binding SelectedStructObject}"
ItemsSource="{Binding StructObjects, Mode=TwoWay}"
HorizontalAlignment="Left"
HorizontalContentAlignment="Left">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<i:InvokeCommandAction
Command="{Binding Command}"
CommandParameter="{Binding ElementName=StructListView, Path=SelectedItem}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
...
MainViewModel:
public class MainViewModel : BaseViewModel
{
private StructElement _selectedStructObject;
public StructElement SelectedStructObject
{
get { return _selectedStructObject; }
set
{
if (_selectedStructObject != value)
{
_selectedStructObject = value;
OnPropertyChanged();
}
}
}
public RelayCommand Command { get; set; }
public ObservableCollection<StructElement> StructObjects { get; set; }
private StructElement _structElement;
public StructElement StructObject
{
get { return _structElement; }
set
{
if (_structElement != value)
{
_structElement = value;
OnPropertyChanged();
}
}
}
public MainViewModel()
{
Command = new RelayCommand(o => { //Method; });
StructObjects = new ObservableCollection<StructElement>();
}
}
RelayCommand:
public class RelayCommand : ICommand
{
private readonly Action<object> execute;
private readonly Predicate<object> canExecute;
public RelayCommand(Action<object> execute)
: this(execute, null)
{
}
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
{
throw new ArgumentNullException("execute");
}
this.execute = execute;
this.canExecute = canExecute;
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public bool CanExecute(object parameter)
{
if (canExecute == null)
{
return true;
}
return canExecute(parameter);
}
public void Execute(object parameter)
{
execute(parameter);
}
}
我知道如果我工作我可以用类似事件:
void OnMouseDoubleClick(Object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left)
{
// Method
}
}
但是因为我正在使用RelayCommand,我没有像MouseButtonEventArgs这样的事件?我想到了"触发"。OnMouseDoubleClick与RelayCommand?
我对编程和WPF很熟悉,所以如果你有关于我应该阅读的东西的进一步信息来理解解决方案,我会很感激的。
谢谢。
我遇到了一个与链接不同的解决方案。
在我的XAML中,我使用"EventToCommand"从MVVMLight:
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<cmd:EventToCommand Command="{Binding LightCommand}"
PassEventArgsToCommand="True"/>
</i:EventTrigger>
</i:Interaction.Triggers>
我使我的RelayCommand通用,所以我可以传递一个事件(我希望我得到了正确的描述):
public class RelayCommand : ICommand
{
private readonly Action<object> execute;
private readonly Predicate<object> canExecute;
public RelayCommand(Action<object> execute)
: this(execute, null)
{
}
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
{
throw new ArgumentNullException("execute");
}
this.execute = execute;
this.canExecute = canExecute;
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public bool CanExecute(object parameter)
{
if (canExecute == null)
{
return true;
}
return canExecute(parameter);
}
public void Execute(object parameter)
{
execute(parameter);
}
}
在我的MainViewModel中,我声明了一个带有MouseButtonEventArgs的RelayCommand,并传递了一个方法来检查MouseButton是否是左键并做一些事情:
public RelayCommand<MouseButtonEventArgs> LightCommand { get; set; }
public void dosomething(MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left)
{
MessageBox.Show(e.OriginalSource.GetType().ToString());
}
}
public MainViewModel()
{
LightCommand = new RelayCommand<MouseButtonEventArgs>(dosomething);
}