gpt4 book ai didi

wpf - 从一个用户控件触发命令并在另一个用户控件的 View 模型中处理它

转载 作者:行者123 更新时间:2023-12-03 10:43:23 27 4
gpt4 key购买 nike

在 Commands.cs 中,定义了一个命令:

public static readonly RoutedUICommand Account_OpenDetails 
= new RoutedUICommand("show account details",
"Account_OpenDetails",
typeof(MainWindow));

UserControl1View我有:
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonDown">
<i:InvokeCommandAction Command="LocalCommands:Command.Account_OpenDetails"
CommandParameter="{Binding AccountId}" />
</i:EventTrigger>
</i:Interaction.Triggers>

UserControl2View我想做这个:
<UserControl.CommandBindings>
<CommandBinding Command="LocalCommands:Command.Account_OpenDetails"
Executed="{Binding Account_OpenDetails}" />
</UserControl.CommandBindings>

我希望这会在 UserControl2ViewModel 中调用以下方法(这是 UserControl2View 的 DataContext ):
public void Account_OpenDetails(object sender, ExecutedRoutedEventArgs e)

相反,我得到:

Unable to cast object of type 'System.Reflection.RuntimeEventInfo' to type 'System.Reflection.MethodInfo'.



基本上,我想从一个用户控件的 XAML 触发一个命令,该命令在另一个用户控件的 View 模型中处理。我可以在 .cs 中处理这个问题的 UserControl2View ,但我不想。我希望它在 View 模型中处理。

什么是正确的语法?只要我实现相同的行为,我对任何其他想法都持开放态度。

最佳答案

Basically, from the XAML of one user control I want to trigger a Command, which is handled in the view model of another user control



在主视图模型上创建一个共享 bool 标志,该标志将标志第二个控件。跟着这些步骤。
  • 创建通知 Boolean主视图模型上的标志(例如 IsAccountDetailsOpen )。
  • 创建 Boolean依赖属性 使用更改处理程序 在二级控制上。在更改处理程序方法中,根据您提到的控件的 View 模型执行需要执行的操作。
  • 在主页上将依赖属性绑定(bind)到 IsAccountDetailsOpen的第二个控制。
  • 在命令代码中,更改 bool 标志 IsAccountDetailsOpentrue当事件发生时向第二个控件发出信号。
  • -0r- 使用双向绑定(bind)在第一个控件上创建 bool 依赖项并设置 IsAccountDetailsOpen为真,向其他控制发出信号。
  • 关于wpf - 从一个用户控件触发命令并在另一个用户控件的 View 模型中处理它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41349068/

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