WPF命令绑定已损坏

时间:2016-04-26 04:18:39

标签: c# wpf

我看到了这个SO question但是当我尝试它时它不起作用。

我知道我可以做mvvm,但执行命令正在做的是视图特定的,所以我希望它在视图上完成。

<Grid>
        <TextBox>
            <TextBox.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="_Copy" CommandTarget="{Binding Path=PlacementTarget, 
          RelativeSource={RelativeSource FindAncestor, 
          AncestorType={x:Type ContextMenu}}}">
                        <MenuItem.CommandBindings>
                            <CommandBinding CanExecute="CommandBinding_CanExecute"
                                                Executed="CommandBinding_Executed"
                                                />
                        </MenuItem.CommandBindings>
                    </MenuItem>
                </ContextMenu>
            </TextBox.ContextMenu>
        </TextBox>
    </Grid>

代码背后:

private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = false;
        }

我希望菜单被禁用但不是:

enter image description here

我尝试了其他方法:

<!--Approach 2: CopyCommand as property of the Window. CopyCommand is in code-behind of the window-->
<MenuItem Header="_Copy" Command="{Binding Path=CopyCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"/>

<!--Approach 3: Saw other samples on the net about binding to PlacementTarget. CopyCommand is in code-behind of the window-->
<MenuItem Header="_Copy" Command="{Binding Path=PlacementTarget.CopyCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}"/>

3 个答案:

答案 0 :(得分:0)

如果您正在尝试实施WPF内置ApplicationCommands.Copy属性,那么您需要做的就是

<TextBox>
    <TextBox.ContextMenu>
        <ContextMenu>
                <MenuItem Command="ApplicationCommands.Copy"/>
        </ContextMenu>
    </TextBox.ContextMenu>
</TextBox>

由于该命令与文本框相关联,因此它将自动处理执行并可以执行事件。

但是,如果你想实现自己的自定义命令,你必须有点冗长

public static class MyCommands
{
    public static readonly RoutedUICommand Copy= new RoutedUICommand
            (
                    "Copy",
                    "_Copy",
                    typeof(MyCommands),
                   null
            );

    //You can define more commands here
}

private void CommandBinding_CanExecute(object sender,  CanExecuteRoutedEventArgs e)
{
        e.CanExecute = true; //can check with debugger...
}

private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
     //Your Action   
}
你的xaml

中的

<Window.CommandBindings>
    <CommandBinding Command="local:MyCommands.Copy" CanExecute="CommandBinding_CanExecute" Executed="CommandBinding_Executed" />
</Window.CommandBindings>
<Grid>

    <TextBox>
        <TextBox.ContextMenu>
            <ContextMenu>
                <MenuItem Command="local:MyCommands.Copy"/>
            </ContextMenu>
        </TextBox.ContextMenu>
    </TextBox>
</Grid>

答案 1 :(得分:0)

我在后面的视图代码中有我的CopyCommand。

查看:

public ICommand CopyCommand { get { return _copyCommand; } }

然后,当我将视图的实例放在textbox的tag属性中时,我能够访问它here

<views:myView>

<Grid>
<TextBox Tag="{Binding RelativeSource={RelativeSource AncestorType={x:Type views:myView}}}">
            <TextBox.ContextMenu>
                <ContextMenu DataContext="{Binding Path=PlacementTarget.Tag, RelativeSource={RelativeSource Self}}">
                        <MenuItem Header="_Copy" Command="{Binding CopyCommand}"/>
                    </ContextMenu>
            </TextBox.ContextMenu>
        </TextBox>
</views:myView>

</Grid>

答案 2 :(得分:-1)

Command中指定MenuItem,如下所示:

<MenuItem Command="ApplicationCommands.Copy" .../>

还在Command

中指定CommandBinding属性的值
<MenuItem.CommandBindings>
     <CommandBinding Command="ApplicationCommands.Copy" CanExecute="CommandBinding_CanExecute"
                     Executed="CommandBinding_Executed" />
</MenuItem.CommandBindings>