您应该使用默认的ApplicationCommands,它们将具有您正在寻找的行为。如果您使用 aToolBar
它将工作而无需执行任何其他操作,否则您将不得不使用FocusManager.IsFocusScope属性或直接绑定CommandTarget
。请注意,如果剪贴板中有任何内容,则粘贴按钮将被启用。您可以使用ClipBoard.Clear
方法来重置剪贴板。
IE:
示例 1
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel >
<ToolBar>
<Button Command="ApplicationCommands.Copy">Copy</Button>
<Button Command="ApplicationCommands.Paste">Paste</Button>
</ToolBar>
<TextBox BorderBrush="Black" BorderThickness="2" Margin="5" TextWrapping="Wrap" />
<TextBox BorderBrush="Black" BorderThickness="2" Margin="5" TextWrapping="Wrap" />
</StackPanel >
</Window>
示例 2
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel >
<TextBox BorderBrush="Black" BorderThickness="2" Margin="5" TextWrapping="Wrap" />
<TextBox BorderBrush="Black" BorderThickness="2" Margin="5" TextWrapping="Wrap" />
<Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch" FocusManager.IsFocusScope="True" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button Grid.Column="0" Height="50" Command="ApplicationCommands.Copy" >Copy</Button>
<Button Grid.Column="1" Height="50" Command="ApplicationCommands.Paste">Paste</Button>
</Grid >
</StackPanel >
</Window>