首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WPF命令绑定中断

WPF命令绑定中断
EN

Stack Overflow用户
提问于 2016-04-26 04:18:38
回答 3查看 370关注 0票数 0

我看到了这个,所以问题,但当我尝试它不起作用。

我知道我可以执行mvvm,但是execute命令所做的是特定于视图的操作,所以我希望在视图上执行。

代码语言:javascript
复制
<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>

代码背后:

代码语言:javascript
复制
private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = false;
        }

我预计菜单将被禁用,但它不是:

我试过其他方法:

代码语言:javascript
复制
<!--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}}"/>
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-05-08 11:53:52

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

查看:

代码语言:javascript
复制
public ICommand CopyCommand { get { return _copyCommand; } }

然后,当我将视图的实例作为指定的这里放在textbox的tag属性中时,我能够访问它

代码语言:javascript
复制
<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>
票数 0
EN

Stack Overflow用户

发布于 2016-04-26 07:31:36

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

代码语言:javascript
复制
<TextBox>
    <TextBox.ContextMenu>
        <ContextMenu>
                <MenuItem Command="ApplicationCommands.Copy"/>
        </ContextMenu>
    </TextBox.ContextMenu>
</TextBox>

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

但是,如果您想实现您自己的自定义命令,则必须稍微冗长一些。

代码语言:javascript
复制
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中

代码语言:javascript
复制
<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>
票数 0
EN

Stack Overflow用户

发布于 2016-04-26 05:28:24

Command中指定MenuItem,如下所示:

代码语言:javascript
复制
<MenuItem Command="ApplicationCommands.Copy" .../>

还在Command中为CommandBinding属性指定一个值,

代码语言:javascript
复制
<MenuItem.CommandBindings>
     <CommandBinding Command="ApplicationCommands.Copy" CanExecute="CommandBinding_CanExecute"
                     Executed="CommandBinding_Executed" />
</MenuItem.CommandBindings>
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36855188

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档