2017-04-10 64 views
1

我在Windows窗體和c#中使用ReactiveUI。我不確定如何從ReactiveCommand中訪問EventArgs。在ReactiveUI Windows窗體中將EventArgs傳遞給ReactiveCommand

我的觀點:

this.BindCommand(ViewModel, vm => vm.FileDragDropped, v => v.listViewFiles, nameof(listViewFiles.DragDrop)); 

視圖模型:

FileDragDropped = ReactiveCommand.Create(() => 
{ 
    // Do something with DragEventArgs 
    // Obtained from listViewFiles.DragDrop in View 
}); 

如何從ReactiveCommaand FileDragDropped內獲得的DragDrop EventArgs的?

+0

我沒有看到'代碼EventArgs'您發佈。你可以請張貼[mcve]嗎? – Enigmativity

+0

可能重複[MVVM傳遞EventArgs作爲命令參數](http://stackoverflow.com/questions/6205472/mvvm-passing-eventargs-as-command-parameter) – bradgonesurfing

+0

@bradgonesurfing此問題與Windows窗體(不是WPF )。 –

回答

0

您可以直接處理事件並將其傳遞給命令。例如在標準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> 
+1

我正在使用Windows窗體(不是WPF),但第一個模塊也適用於Windows窗體。謝謝! –

+0

如果你很高興,你知道它投票:) :) – bradgonesurfing

+0

只要忽略該XAML部分並使用直接事件綁定。如果答案適合您,那麼您應該將其標記爲已接受。 – bradgonesurfing

相關問題