您可以直接處理事件並將其傳遞給命令。例如在標準WPF中使用標籤並使用ReactiveUI.Events
nuget包。
var rc = ReactiveCommand.Create<DragEventArgs>
(e => Console.WriteLine(e));
this.Events().Drop.Subscribe(e => rc.Execute(e));
,或者如果你想堅持用XAML,然後在附加的行爲
public class DropCommand : Behavior<FrameworkElement>
{
public ReactiveCommand<DragEventArgs,Unit> Command
{
get => (ReactiveCommand<DragEventArgs,Unit>)GetValue(CommandProperty);
set => SetValue(CommandProperty, value);
}
// Using a DependencyProperty as the backing store for ReactiveCommand. This enables animation, styling, binding, etc...
public static readonly DependencyProperty CommandProperty =
DependencyProperty.Register("Command", typeof(ReactiveCommand<DragEventArgs,Unit>), typeof(DropCommand), new PropertyMetadata(null));
// Using a DependencyProperty as the backing store for ReactiveCommand. This enables animation, styling, binding, etc...
private IDisposable _Disposable;
protected override void OnAttached()
{
base.OnAttached();
_Disposable = AssociatedObject.Events().Drop.Subscribe(e=> Command?.Execute(e));
}
protected override void OnDetaching()
{
base.OnDetaching();
_Disposable.Dispose();
}
}
創建和使用它像
<Label>
<i:Interaction.Behaviors>
<c:DropCommand Command="{Binding DropCommand}" />
</i:Interaction.Behaviors>
</Label>
我沒有看到'代碼EventArgs'您發佈。你可以請張貼[mcve]嗎? – Enigmativity
可能重複[MVVM傳遞EventArgs作爲命令參數](http://stackoverflow.com/questions/6205472/mvvm-passing-eventargs-as-command-parameter) – bradgonesurfing
@bradgonesurfing此問題與Windows窗體(不是WPF )。 –