如何将参数传递给事件触发器 wpf 中存在的方法 [英] how to pass argument to the method present in event trigger wpf
本文介绍了如何将参数传递给事件触发器 wpf 中存在的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
实际上,我试图在 Xaml 文件的 ViewModel 中存在的方法 UpdateWord(object obj) 中传递 word 文档的名称.这样它就会打开word文档.
视图模型:
public void UpdateWord(Object obj){//做点什么 ..... ;}
解决方案
有多种方法可以做到这一点,看
使用 GalaSoft.MvvmLight.CommandWpf公共主窗口(){初始化组件();CommandOne = new RelayCommand(executeCommandOne);CommandTwo = new RelayCommand(executeCommandTwo);}public RelayCommandCommandOne { 得到;放;}public RelayCommand CommandTwo { get;放;}私有无效executeCommandOne(字符串参数){//在这里做点什么.}私有无效executeCommandTwo(){//在这里做点什么.}
- 使用 Telerik EventToCommandBehavior.您必须下载它 NuGet 包.这是一个选项.
XAML:
<telerek:EventToCommandBehavior命令="{绑定 DropCommand}"事件="掉落"PassArguments="True"/></i:Interaction.Behaviors>
代码:
public ActionCommandDropCommand { 得到;私人订制;}this.DropCommand = new ActionCommand(OnDrop);私有无效 OnDrop(DragEventArgs e){//做点什么}
Actually, I'm trying to pass the name of the word document in the method UpdateWord(object obj) present in the ViewModel of the Xaml file. So that it will open the word document.
<Button Content="Show Word" Width="100" Height="25" Margin="128,70,22,37">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<si:CallDataMethod Method="UpdateWord"/>
<si:SetProperty TargetName="LayoutRoot"
PropertyName="Background" Value="PaleGoldenrod"/>
</i:EventTrigger>
</i:Interaction.Triggers>
ViewModel :
public void UpdateWord(Object obj)
{
//Do Something ..... ;
}
解决方案
There are multiple ways of doing this, look here:
- Using WPF Tools. Easiest
Add Namespaces:
- System.Windows.Interactivitiy
- Microsoft.Expression.Interactions
XAML:
<Window>
xmlns:wi="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions">
<wi:Interaction.Triggers>
<wi:EventTrigger EventName="SelectionChanged">
<ei:CallMethodAction
TargetObject="{Binding}"
MethodName="ShowCustomer"/>
</wi:EventTrigger>
</wi:Interaction.Triggers>
</Window>
Code:
public void ShowCustomer()
{
// Do something.
}
- Using MVVMLight. Most difficult but best practice
Install GalaSoft NuGet package.
Get the namespaces:
- System.Windows.Interactivity
- GalaSoft.MvvmLight.Platform
XAML
<Window>
xmlns:wi="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:cmd="http://www.galasoft.ch/mvvmlight">
<wi:Interaction.Triggers>
<wi:EventTrigger EventName="Navigated">
<cmd:EventToCommand Command="{Binding NavigatedEvent}"
PassEventArgsToCommand="True" />
</wi:EventTrigger>
</wi:Interaction.Triggers>
</Window>
Code With Delegates: Source
You must get the Prism MVVM NuGet package for this.
using Microsoft.Practices.Prism.Commands;
// With params.
public DelegateCommand<string> CommandOne { get; set; }
// Without params.
public DelegateCommand CommandTwo { get; set; }
public MainWindow()
{
InitializeComponent();
// Must initialize the DelegateCommands here.
CommandOne = new DelegateCommand<string>(executeCommandOne);
CommandTwo = new DelegateCommand(executeCommandTwo);
}
private void executeCommandOne(string param)
{
// Do something here.
}
private void executeCommandTwo()
{
// Do something here.
}
Code Without DelegateCommand: Source
using GalaSoft.MvvmLight.CommandWpf
public MainWindow()
{
InitializeComponent();
CommandOne = new RelayCommand<string>(executeCommandOne);
CommandTwo = new RelayCommand(executeCommandTwo);
}
public RelayCommand<string> CommandOne { get; set; }
public RelayCommand CommandTwo { get; set; }
private void executeCommandOne(string param)
{
// Do something here.
}
private void executeCommandTwo()
{
// Do something here.
}
- Using Telerik EventToCommandBehavior. You'll have to download it's NuGet Package. It's an option.
XAML:
<i:Interaction.Behaviors>
<telerek:EventToCommandBehavior
Command="{Binding DropCommand}"
Event="Drop"
PassArguments="True" />
</i:Interaction.Behaviors>
Code:
public ActionCommand<DragEventArgs> DropCommand { get; private set; }
this.DropCommand = new ActionCommand<DragEventArgs>(OnDrop);
private void OnDrop(DragEventArgs e)
{
// Do Something
}
这篇关于如何将参数传递给事件触发器 wpf 中存在的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文