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

时间:2023-01-20 17:50:17

I have created a toolbar that has buttons.

我创建了一个包含按钮的工具栏。

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.

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

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

2 个解决方案

#1


5  

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.

你不能这样使用命令! Command(以您使用它的方式)应位于菜单或工具栏中。顺便说一下,你不需要那些点击事件处理程序,因为你要使用命令!我建议您尝试将DelegateCommand添加到ViewModel并让该委托调用ApplicationCommads。

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

我强烈建议您阅读此http://msdn.microsoft.com/en-us/magazine/dd419663.aspx但作为快速解决方案,您可以尝试以下方法(重要的是:请记住,您需要选择一些文本) TextBox然后将启用复制和剪切):

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

#2


0  

For the purpose you are trying to achieve I would suggest using a togglebutton.

为了达到目的,我建议使用togglebutton。

Also the button would be clickable as and when they are supposed to. For example

此按钮也可以按原样点击。例如

  • Paste button gets clickable only when there is something to paste.
  • 只有在要粘贴的内容时,粘贴按钮才会被点击。
  • Cut/Copy Button gets clickable when there something selected in RTB.
  • 在RTB中选择某些内容时,剪切/复制按钮可以单击。

Have a look at all the ApplicationCommands at msdn. You can implement them as easily as:

看一下msdn上的所有ApplicationCommands。您可以像以下一样轻松地实现它们:

<ToggleButton x:Name="PasteBtn" Command="ApplicationCommands.Paste"/>
<ToggleButton x:Name="CutBtn"   Command="ApplicationCommands.Cut"/>
<ToggleButton x:Name="CopyBtn"  Command="ApplicationCommands.Copy"/>

#1


5  

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.

你不能这样使用命令! Command(以您使用它的方式)应位于菜单或工具栏中。顺便说一下,你不需要那些点击事件处理程序,因为你要使用命令!我建议您尝试将DelegateCommand添加到ViewModel并让该委托调用ApplicationCommads。

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

我强烈建议您阅读此http://msdn.microsoft.com/en-us/magazine/dd419663.aspx但作为快速解决方案,您可以尝试以下方法(重要的是:请记住,您需要选择一些文本) TextBox然后将启用复制和剪切):

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

#2


0  

For the purpose you are trying to achieve I would suggest using a togglebutton.

为了达到目的,我建议使用togglebutton。

Also the button would be clickable as and when they are supposed to. For example

此按钮也可以按原样点击。例如

  • Paste button gets clickable only when there is something to paste.
  • 只有在要粘贴的内容时,粘贴按钮才会被点击。
  • Cut/Copy Button gets clickable when there something selected in RTB.
  • 在RTB中选择某些内容时,剪切/复制按钮可以单击。

Have a look at all the ApplicationCommands at msdn. You can implement them as easily as:

看一下msdn上的所有ApplicationCommands。您可以像以下一样轻松地实现它们:

<ToggleButton x:Name="PasteBtn" Command="ApplicationCommands.Paste"/>
<ToggleButton x:Name="CutBtn"   Command="ApplicationCommands.Cut"/>
<ToggleButton x:Name="CopyBtn"  Command="ApplicationCommands.Copy"/>