如何将菜单上下文项的命令单击从一个用户控件绑定到 WPF 中的其他用户控件



在我的代码中,我有以下用户控件

  1. 我的列表用户控件
  2. MyListItemUserControl

MyListUserControl 加载列表,其中包含从 MyListItemUserControl 加载的项,

在MyListItemUserControl上,我有一个按钮,单击它时我正在显示具有多个选项的上下文菜单。

现在单击该菜单项时,我必须从MyListUserControl调用方法,是否可以这样做?

简而言之,以下是我的结构

  MyListUserControl -> MyListItemUserControl -> ContextMenu Item Click -> Command from MyListUserControl 

如果我在同一个列表用户控制中编写列表项代码,它工作正常。但我更喜欢将项目和列表代码放在单独的类中。

MyListUserControl xaml Code

    <ListView ItemsSource="{Binding Data}">
           <ListView.ItemTemplate>
                 <DataTemplate>
                       <local1:MyListItemUserControl HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch"
                           CommentData="{Binding}"/>
                                </DataTemplate>
            </ListView.ItemTemplate>
     </ListView>

我在此类中定义了 DelegateCommand ,我想从 MyListItemUserControl 调用它

MyListItemUserControl xaml

<ContextMenu>
     <MenuItem Command="{Binding Source={StaticResource Proxy}, Path=Data.MenuItem_Clicked}"
                            Header="{x:Static propertyRes:Resources.Txt_Copy}"
                            Style="{StaticResource menuItems}" />
     <MenuItem Header="{x:Static propertyRes:Resources.Txt_Edit}" Style="{StaticResource menuItems}" />
     <MenuItem Header="{x:Static propertyRes:Resources.Txt_Delete}" Style="{StaticResource menuItems}" />
</ContextMenu>
这是

伪代码,但应该足以让你明白这个想法

<MyListItemUserControl>
    <MyListItemUserControl.ItemTemplate>
        <Grid>
            <Grid.ContextMenu>
                <ContextMenu>
                    <ContextMenuItem 
                    Header="Do stuff"
                    Command="{Binding ElementName=DetailsView, Path=DataContext.DoStuffCommand" 
                    CommandParameter="{Binding}"/>
                </ContextMenu>
            </Grid.ContextMenu>
            <!-- your content -->
        </Grid>
    </<MyListItemUserControl.ItemTemplate>
</MyListItemUserControl>
<MyListItemUserControl x:Name="DetailsView">
<!-- I assume that data context is MyListItemViewModel or something similar
   it has the command: DataContext.DoStuff that takes items from list as parameter -->
</MyListItemUserControl>

编辑:

如果您的控件是在单独的文件中定义的,则只需传递 commnad。我通常通过在代码后面添加一个包含命令的依赖项属性来执行此操作:

 public static readonly DependencyProperty DoStuffCommandProperty = DependencyProperty.Register(
            "DoStuffCommand", typeof(ICommand), typeof(MainWindow), new PropertyMetadata(default(ICommand)));
        public ICommand DoStuffCommand
        {
            get { return (ICommand) GetValue(DoStuffCommandProperty); }
            set { SetValue(DoStuffCommandProperty, value); }
        }

所以你可以做到:

<ListView ItemsSource="{Binding Data}">
           <ListView.ItemTemplate>
                 <DataTemplate>
                       <local1:MyListItemUserControl HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch"
                           CommentData="{Binding}" DoStuffCommand="{Binding DoStuffCommandInSomeViewModel}"/>
                                </DataTemplate>
            </ListView.ItemTemplate>
     </ListView>

最新更新