简体   繁体   English

WPF 从页面内的 UserControl 在 MainWindow 上执行 ICommand

[英]WPF Executing an ICommand on MainWindow from a UserControl inside a Page

I'm showing a list of Orders, (each Order is a UserControl), in more than one Page.我在多个页面中显示了一个订单列表(每个订单都是一个 UserControl)。 Every time the user left-click the control, main window should load & show the order in a new Page using an ICommand.每次用户左键单击控件时,main window 应该使用 ICommand 在新页面中加载和显示订单。

x:Name of MainWindow is: mainWindow x:MainWindow 的名称是: mainWindow

MainViewModel主视图模型

This is de view model associated to the MainWindow.这是与 MainWindow 关联的视图 model。

public class MainViewModel : BaseViewModel
{
    public INavigator Navigator { get; set; }
    public ICommand OpenOrderCommand { get; set; }

    public MainViewModel(INavigator navigator) : base()
    {
        Navigator = navigator;
        OpenOrderCommand = new RelayCommand(OpenOrder);
    }

    private void OpenOrder(object obj)
    {
        // Loads the order from db
    }
}

I've added an InputBindings tag in the UserControl to detect when the user left-click it.我在 UserControl 中添加了一个 InputBindings 标签来检测用户何时左键单击它。

UserControl用户控件

<Grid.InputBindings>
    <MouseBinding MouseAction="LeftClick" 
        Command="{Binding DataContext.OpenOrderCommand,
                          RelativeSource={RelativeSource Mode=FindAncestor, 
                          AncestorType={x:Type Window}}}"
        CommandParameter="{Binding OrderId}"/>
</Grid.InputBindings>

But OpenOrderCommand is never fired, and I get a XAML binding failure with this text:但是OpenOrderCommand永远不会被触发,我得到一个 XAML 绑定失败的文本:

System.Windows.Data Error: 4: Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Window', AncestorLevel='1''. System.Windows.Data 错误:4:无法找到参考“RelativeSource FindAncestor,AncestorType='System.Windows.Window',AncestorLevel='1'”的绑定源。 BindingExpression:Path=DataContext.OpenOrderCommand; BindingExpression:Path=DataContext.OpenOrderCommand; DataItem=null;数据项=空; target element is 'MouseBinding' (HashCode=20815867);目标元素是 'MouseBinding' (HashCode=20815867); target property is 'Command' (type 'ICommand')目标属性是“命令”(类型“ICommand”)

How can I execute a ICommand of MainWindow from a UserControl?如何从 UserControl 执行 MainWindow 的 ICommand?

UPDATE更新

It works fine if I use current Page instead of MainWindow.如果我使用当前页面而不是 MainWindow,它工作正常。

<Grid.InputBindings>
    <MouseBinding MouseAction="LeftClick" 
        Command="{Binding DataContext.OpenOrderCommand,
                          RelativeSource={RelativeSource Mode=FindAncestor, 
                          AncestorType={x:Type local:HomePage}}}"
        CommandParameter="{Binding OrderId}"/>
</Grid.InputBindings>

In the error message it says it can't find the binding source.在错误消息中,它说找不到绑定源。 Try to bind it like this:尝试像这样绑定它:

<Grid.InputBindings>
        <MouseBinding MouseAction="LeftClick" 
                      Command="{Binding OpenOrderCommand}"
                      CommandParameter="{Binding OrderId}"/>
</Grid.InputBindings>

and to set the DataContext like this:并像这样设置 DataContext:

public MainWindow()
{
     InitializeComponent();
     DataContext = new MainViewModel();
}

In my test code the Grid only raised the event when I had explecitly set a value for the Background-Property.在我的测试代码中,Grid 仅在我为 Background-Property 明确设置值时引发事件。 Transparent was also a value which worked.透明也是一个有效的价值观。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM