gpt4 book ai didi

c# - WPF 和 MVVM : passing event args don't work

转载 作者:行者123 更新时间:2023-12-03 11:03:32 25 4
gpt4 key购买 nike

我有 WPF MVVM 应用程序,我想将事件参数传递到 ViewModel 中,但它不起作用。我浏览了在这个网站和其他网站上找到的一堆类似的主题,但没有结果。

这是我在控件中的 XAML:

<telerik:RadMap>
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseMove" >
<ei:CallMethodAction TargetObject="{Binding}" MethodName="VM.SomeMethod"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</telerik:RadMap>

在我的 ViewModel 内部,我有以下方法:
public void SomeMethod(object sender, MouseEventArgs e)
{

}

当我将鼠标移到控件上时,它会引发异常,指出该方法没有适当的签名。

然后我也尝试使用 Telerik 的 EventToCommandBehavior,像这样:
<telerik:RadMap>
<telerik:EventToCommandBehavior ...>
...

它提示 EventToCommandBehavior 不存在。我用谷歌搜索,甚至在 Telerik 文档中也发现了这个 EventToCommandBehavior。所以我猜测这可能是一个较新的功能,因为我的 Telerik 是从 2010 年开始的。

无论如何,欢迎任何关于如何将事件参数传递给 ViewModel 的建议。

最佳答案

编辑 : 我应该更好地阅读这个问题。绑定(bind)表达式的默认根是被绑定(bind)的 UI 元素的 DataContext,因此绑定(bind)表达式将 VM.SomeCommand 读取为 DataContext.VM.SomeCommand。 VM.SomeCommand 应该更改为只是 SomeCommand,因为您的 View 的 DataContext 应该是您的 ViewModel。

Expression Blend 交互性框架不支持将事件参数传递给命令。我建议使用 MVVM 框架。

MVVM Light ,布拉德利乌夫纳提到,你可以这样做:

<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseMove">
<cmd:EventToCommand Command="{Binding Mode=OneWay, Path=SomeCommand}"
PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>

但是,如果您出于某种原因反对该想法,您可以尝试讨论的解决方案 here .

链接中的示例代码:
public sealed class InvokeDelegateCommandAction : TriggerAction<DependencyObject>
{
/// <summary>
///
/// </summary>
public static readonly DependencyProperty CommandParameterProperty =
DependencyProperty.Register("CommandParameter", typeof(object), typeof(InvokeDelegateCommandAction), null);

/// <summary>
///
/// </summary>
public static readonly DependencyProperty CommandProperty = DependencyProperty.Register(
"Command", typeof(ICommand), typeof(InvokeDelegateCommandAction), null);

/// <summary>
///
/// </summary>
public static readonly DependencyProperty InvokeParameterProperty = DependencyProperty.Register(
"InvokeParameter", typeof(object), typeof(InvokeDelegateCommandAction), null);

private string commandName;

/// <summary>
///
/// </summary>
public object InvokeParameter
{
get
{
return this.GetValue(InvokeParameterProperty);
}
set
{
this.SetValue(InvokeParameterProperty, value);
}
}

/// <summary>
///
/// </summary>
public ICommand Command
{
get
{
return (ICommand)this.GetValue(CommandProperty);
}
set
{
this.SetValue(CommandProperty, value);
}
}

/// <summary>
///
/// </summary>
public string CommandName
{
get
{
return this.commandName;
}
set
{
if (this.CommandName != value)
{
this.commandName = value;
}
}
}

/// <summary>
///
/// </summary>
public object CommandParameter
{
get
{
return this.GetValue(CommandParameterProperty);
}
set
{
this.SetValue(CommandParameterProperty, value);
}
}

/// <summary>
///
/// </summary>
/// <param name="parameter"></param>
protected override void Invoke(object parameter)
{
this.InvokeParameter = parameter;

if (this.AssociatedObject != null)
{
ICommand command = this.ResolveCommand();
if ((command != null) && command.CanExecute(this.CommandParameter))
{
command.Execute(this.CommandParameter);
}
}
}

private ICommand ResolveCommand()
{
ICommand command = null;
if (this.Command != null)
{
return this.Command;
}
var frameworkElement = this.AssociatedObject as FrameworkElement;
if (frameworkElement != null)
{
object dataContext = frameworkElement.DataContext;
if (dataContext != null)
{
PropertyInfo commandPropertyInfo = dataContext
.GetType()
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.FirstOrDefault(
p =>
typeof(ICommand).IsAssignableFrom(p.PropertyType) &&
string.Equals(p.Name, this.CommandName, StringComparison.Ordinal)
);

if (commandPropertyInfo != null)
{
command = (ICommand)commandPropertyInfo.GetValue(dataContext, null);
}
}
}
return command;
}
}

来自博客的示例用法:
<ComboBox>
<ComboBoxItem Content="Foo option 1" />
<ComboBoxItem Content="Foo option 2" />
<ComboBoxItem Content="Foo option 3" />
<Interactivity:Interaction.Triggers>
<Interactivity:EventTrigger EventName="SelectionChanged" >
<Presentation:InvokeDelegateCommandAction
Command="{Binding SubmitFormCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=InvokeParameter}" />
</Interactivity:EventTrigger>
</Interactivity:Interaction.Triggers>
</ComboBox>

注意:我尚未测试此博客中的解决方案。

关于c# - WPF 和 MVVM : passing event args don't work,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42438067/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com