WPF上下文菜单itemtemplate命令参数绑定返回null [英] WPF Contextmenu itemtemplate commandParameter binding returns null

查看:81
本文介绍了WPF上下文菜单itemtemplate命令参数绑定返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题.我正在尝试在wpf中创建具有数据绑定的上下文菜单.上下文菜单项将绑定到可观察对象的集合.上下文菜单的填充很好-但是,我想向其中添加命令.

I have a problem. I am trying to create a context menu with databinding in wpf. The context menu items would are bound to an observable collection of objects. The population of context menu is fine - however, I want to add the command to it.

我的使用方式是:

在XAML中

 <Grid.Resources>
            <local:RestoreCommand x:Key="RestoreCommand" />
            <local:ShowBalloonCommand x:Key="BaloonCommand" />
            <local:StartTaskCommand x:Key="StartTaskCommand" />
        </Grid.Resources>

<ContextMenu ItemsSource="{Binding}" Name="taskBarContextMenu">
                    <ContextMenu.ItemContainerStyle>
                        <Style TargetType="MenuItem">
                            <Setter Property="Command" Value="{StaticResource StartTaskCommand}"/>
                            <Setter Property="CommandParameter" Value="{Binding mainWindow}"/>
                        </Style>
                    </ContextMenu.ItemContainerStyle>

<TextBlock TextWrapping="Wrap" Text="{Binding Name}" Grid.Column="0"/>
                                    <TextBlock TextWrapping="Wrap" Text="{Binding TimeElapsed, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Grid.Column="2"/>

和在C#中:

taskbarIcon.ContextMenu.ItemsSource = TasksList;

我有一个用于命令的类

public class StartTaskCommand : ICommand
    {
        public void Execute(object parameter)
        {
            //THE PARAMETER IS ALWAYS NULL
            var window = parameter as MainWindow;
            if (window != null)
            {

            }
        }

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public event EventHandler CanExecuteChanged;
    } 

我对其他两个命令进行了类似设置,并且工作正常.我尝试添加为该参数的任何内容始终为null-是taskBarContextMenu还是mainWindow或menuItem ...任何想法都欢迎.

I have it set up simiarly for other two commands and it works fine. Anything I try to add as the parameter is always null - be it taskBarContextMenu, or mainWindow or menuItem... Any ideas welcome.

====

我尝试了以下建议的解决方案,但是命令参数仍然为空:

I tried a following solution as suggested, but the command parameter is still null:

 <tb:TaskbarIcon Name="taskbarIcon"
            DoubleClickCommand="{StaticResource RestoreCommand}"
            DoubleClickCommandParameter="{Binding ElementName=mainWindow}"

            LeftClickCommand="{StaticResource BaloonCommand}"
            LeftClickCommandParameter="{Binding ElementName=mainWindow}"
            IconSource=".\256px-Out_of_date_clock_icon.ico"
                        MenuActivation="RightClick"
                        Tag="{Binding ElementName=mainWindow}"
                        >



<tb:TaskbarIcon.ContextMenu ><ContextMenu DataContext="{Binding Path=PlacementTarget.Tag, RelativeSource={RelativeSource Self}}" Tag="{Binding}" Name="taskBarContextMenu">
            <ContextMenu.ItemContainerStyle>
                <Style TargetType="MenuItem">
                    <Setter Property="Command" Value="{StaticResource StartTaskCommand}"/>
                    <Setter Property="CommandParameter" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.DataContext}"/>
                </Style>
            </ContextMenu.ItemContainerStyle>

推荐答案

您需要在菜单容器中添加标签,然后使用放置目标将其绑定到菜单.

You need to add a tag to the menu's container and bind to it using placement target.

查看此示例:

<StackPanel x:Key="ConfigurationListItem" x:Shared="False" Tag="{Binding ElementName=UserControl}">
        <StackPanel Orientation="Horizontal">
            <Button>
                <Button.InputBindings>
                    <MouseBinding Gesture="LeftDoubleClick" Command="{Binding ElementName=UserControl, Path=LaunchCommand}" CommandParameter="{Binding}" />
                    <MouseBinding Gesture="LeftClick" Command="{Binding ElementName=UserControl, Path=SelectCommand}" CommandParameter="{Binding}" />
                </Button.InputBindings>
        </StackPanel>

        <StackPanel.ContextMenu>
            <ContextMenu DataContext="{Binding Path=PlacementTarget.Tag, RelativeSource={RelativeSource Self}}" Tag="{Binding}">
                <MenuItem Header="Sync Environment Dependencies" 
                        Command="{Binding Parent.PlacementTarget.Tag.SyncEnvironmentCommand, RelativeSource={RelativeSource Self}}"
                        CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.DataContext}" />
            </ContextMenu>
        </StackPanel.ContextMenu>
    </StackPanel>

这篇关于WPF上下文菜单itemtemplate命令参数绑定返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆