具有多种目标类型的 WPF 命令

发布于 2024-10-11 11:39:33 字数 1328 浏览 5 评论 0原文

我对具有不同目标类型的 WPF 命令有点困惑。

因此,如果我定义一个命令

<Window.CommandBindings>
        <CommandBinding Command="Copy"
                        Executed="CopyCmdExecuted"
                        CanExecute="CopyCmdCanExecute"/>

    </Window.CommandBindings>

现在我在上下文菜单中使用它:

                    <ContextMenu Name="FolderContextMenu">
                        <MenuItem Command="Copy"/>
                        </ContextMenu>

我有一个方法来处理该命令:

private void CopyCmdExecuted(object sender, ExecutedRoutedEventArgs e)
{


}

我在一个普通的旧菜单中使用它:

    <Menu  Name="editMenu">
        <MenuItem Command="Copy"/>
    </Menu>

我理解这一点没有问题。但我有点困惑,如果目标对象是不同类型,我应该做什么。

假设我有文件夹和用户,它们都有一个带有“新建”命令的上下文菜单(以及菜单栏编辑菜单,其中也有“新建”命令)。

当执行New时,无论是Folder还是User,都会执行CopyCmdExecuted。那么,我现在应该对目标进行多路分解吗?就像

   private void CopyCmdExecuted(object sender, ExecutedRoutedEventArgs e)
        {
           if(sender is User)
               // Do copy user stuff
           if(sender is Folder)
               // Do copy folder stuff
        }

如果我最终想要复制很多数据类型,这似乎有点烦人。我在这里不明白什么吗?

(显然,我可以使用 DoCopy 从 Copiable 基类继承文件夹和用户,但这似乎仍然是错误的。)

I'm a bit confused about WPF commands with different target types.

So if I define a command

<Window.CommandBindings>
        <CommandBinding Command="Copy"
                        Executed="CopyCmdExecuted"
                        CanExecute="CopyCmdCanExecute"/>

    </Window.CommandBindings>

And now I use it in a context menu:

                    <ContextMenu Name="FolderContextMenu">
                        <MenuItem Command="Copy"/>
                        </ContextMenu>

And I have a method to handle the command:

private void CopyCmdExecuted(object sender, ExecutedRoutedEventArgs e)
{


}

And I use it in a plain old menu:

    <Menu  Name="editMenu">
        <MenuItem Command="Copy"/>
    </Menu>

I have no problem understanding that. But I'm a bit confused what I am supposed to do if the target objects are different types.

Lets say I have Folders and Users, both of which have a context menu with the New command (and the menu bar edit menu which also has the New command).

When New is executed, regardless of whether its a Folder or a User, CopyCmdExecuted is executed. So, am I now supposed to de-multiplex on the target? Something like

   private void CopyCmdExecuted(object sender, ExecutedRoutedEventArgs e)
        {
           if(sender is User)
               // Do copy user stuff
           if(sender is Folder)
               // Do copy folder stuff
        }

If I end up with lots of data types I wish to copy, it seems a bit annoying. Am I not understanding something here?

(Obviously, I could just have Folder and User inherit from a Copiable base class with DoCopy but that still seems wrong.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沫离伤花 2024-10-18 11:39:33

您可以在调用 Command 时发送 CommandParameter 来指示要应用的命令的含义。这里有两个 TextBlock 元素:

<Grid>
    <StackPanel>
        <TextBlock Name="textBlock1" Text="File">
            <TextBlock.ContextMenu>
                <ContextMenu>
                    <MenuItem
                        Command="Copy"
                        CommandParameter="{Binding PlacementTarget, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}"/>
                </ContextMenu>
            </TextBlock.ContextMenu>
        </TextBlock>
        <TextBlock Name="textBlock2" Text="Folder">
            <TextBlock.ContextMenu>
                <ContextMenu>
                    <MenuItem
                        Command="Copy"
                        CommandParameter="{Binding PlacementTarget, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}"/>
                </ContextMenu>
            </TextBlock.ContextMenu>
        </TextBlock>
    </StackPanel>
</Grid>

并且此代码隐藏:

    private void CopyCmdExecuted(object sender, ExecutedRoutedEventArgs e)
    {
        var text = (e.Parameter as TextBlock).Text;
        Debug.WriteLine("Text = " + text);
    }

使用参数来确定上下文菜单适用于哪个 TextBlock。如果适合您,您也可以只使用字符串 "File""Folder"

You can send a CommandParameter when you invoke the Command to indicate what you mean the command to apply to. Here's are two TextBlock elements:

<Grid>
    <StackPanel>
        <TextBlock Name="textBlock1" Text="File">
            <TextBlock.ContextMenu>
                <ContextMenu>
                    <MenuItem
                        Command="Copy"
                        CommandParameter="{Binding PlacementTarget, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}"/>
                </ContextMenu>
            </TextBlock.ContextMenu>
        </TextBlock>
        <TextBlock Name="textBlock2" Text="Folder">
            <TextBlock.ContextMenu>
                <ContextMenu>
                    <MenuItem
                        Command="Copy"
                        CommandParameter="{Binding PlacementTarget, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}"/>
                </ContextMenu>
            </TextBlock.ContextMenu>
        </TextBlock>
    </StackPanel>
</Grid>

and this code-behind:

    private void CopyCmdExecuted(object sender, ExecutedRoutedEventArgs e)
    {
        var text = (e.Parameter as TextBlock).Text;
        Debug.WriteLine("Text = " + text);
    }

uses the parameter to figure out which TextBlock the context menu applies to. You can also just use the strings "File" and "Folder" if that works for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文