WPF Contextmenu itemtemplate commandParameter binding返回null

时间:2015-03-12 13:23:28

标签: c# wpf xaml contextmenu

我有问题。我试图在wpf中创建一个带数据绑定的上下文菜单。上下文菜单项将绑定到可观察的对象集合。上下文菜单的数量很好 - 但是,我想将命令添加到它。

我使用它的方式是:

在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 ...... 欢迎任何想法。

====

我按照建议尝试了以下解决方案,但命令参数仍然为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>

2 个答案:

答案 0 :(得分:0)

看起来您的ans是this

这谈到了一些已知的问题。

答案 1 :(得分:0)

您需要在菜单的容器中添加标签,并使用展示位置目标绑定到该容器。

查看此示例:

<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>