使用WPF按钮复制和粘贴命令

使用WPF按钮复制和粘贴命令

本文介绍了使用WPF按钮复制和粘贴命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个带有按钮的工具栏。

I have created a toolbar that has buttons.

其中的3个按钮是剪切复制并粘贴的。我设置了每个按钮的命令以剪切复制并粘贴到属性上,但是当我运行程序时,甚至都没有可单击的按钮。我猜他们是残疾人士吗?我正在尝试从文本框复制并粘贴到tabcontrol中的文本框。任何帮助表示赞赏。

Of the buttons 3 of them are cut copy and paste. I set the command of each of those buttons to cut copy and paste on the properties but when I go run the program none of the buttons are even clickable. Are they disabled I'm guessing? I'm trying to copy and paste from textbox to textbox in a tabcontrol. Any help is appreciated.

<Style TargetType="{x:Type Button}" x:Key="textBoxCommands">
  <Setter Property="Content"
          Value="{Binding RelativeSource={RelativeSource Self},
                          Path=Command.Text}" />
  <Setter Property="CommandTarget"
          Value="{Binding ElementName=textBox}" />
</Style>

<Button x:Name="btnCut"
        Click="btnCut_Click">
  <Image Source="Icons/Cut.png" ToolTip="Cut" />
</Button>
<Button x:Name="btnCopy"
        Click="btnCopy_Click"
        Command="ApplicationCommands.Copy"
        Style="{StaticResource textBoxCommands}">
  <Image Source="Icons/Copy.png" ToolTip="Copy" />
</Button>
<Button x:Name="btnPaste"
        Click="btnPaste_Click"
        Command="ApplicationCommands.Paste"
        Style="{StaticResource textBoxCommands}" >
  <Image Source="Icons/Paste.png" ToolTip="Paste" />
</Button>


推荐答案

您不能以这种方式使用命令!该命令(使用方式)应位于菜单工具栏中。

顺便说一句,由于您将要使用Commands,因此不需要那些click事件处理程序!

我建议您尝试将 DelegateCommand 添加到ViewModel,然后让该委托调用 ApplicationCommads

You can’t use command this way! The Command (in the way you use it) should be inside a Menu or Toolbar.
By the way, you don’t need those click event handler since you are going to use Commands!
I recommend you to try add DelegateCommand to the ViewModel and let that delegate call ApplicationCommads.

我强烈建议您阅读此

但是,作为一种快速的解决方案,请尝试以下操作(重要:记住,您需要在 TextBox 中选择一些文本,然后 Copy Cut 将被启用):

I highly recommend you to read this http://msdn.microsoft.com/en-us/magazine/dd419663.aspx
But as a quick solution for you try the following (important: remember that you need to have some text selected in your TextBox then Copy and Cut will be enabled):

<StackPanel  HorizontalAlignment="Left" VerticalAlignment="Top">

  <ToolBar>
    <Button Content="Cut" Command="ApplicationCommands.Cut" Height="23" Width="75"/>
    <Button Content="Copy" Command="ApplicationCommands.Copy" Height="23" Width="75"/>
    <Button Content="Paste" Command="ApplicationCommands.Paste" Height="23" Width="75"/>
  </ToolBar>

  <TextBox Height="23" Name="textBox1" Width="120"/>

</StackPanel>

这篇关于使用WPF按钮复制和粘贴命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 01:12