我试图了解如何使用 RoutedCommands。我的印象是,如果我没有在 Button 上指定 CommandTarget,任何有焦点的元素都会收到命令。但由于某种原因,它不起作用。这是不起作用的xaml:
<Window x:Class="WpfTest11_Commands2.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">
<Grid>
<TextBox Height="177" HorizontalAlignment="Left"
Margin="12,12,0,0" Name="textBox1" VerticalAlignment="Top" Width="233" AcceptsReturn="True" />
<TextBox Height="177" HorizontalAlignment="Left"
Margin="258,12,0,0" Name="textBox2" VerticalAlignment="Top" Width="233" AcceptsReturn="True" />
<Button Content="Cut"
Height="23" HorizontalAlignment="Left" Margin="12,195,0,0" Name="button1" VerticalAlignment="Top" Width="75"
Command="ApplicationCommands.Cut"/>
</Grid>
</Window>
如果我将 CommandTarget 添加到 Button 它可以工作,但仅限于指定的文本框。
<Window x:Class="WpfTest11_Commands2.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">
<Grid>
<TextBox Height="177" HorizontalAlignment="Left" Margin="12,12,0,0" Name="textBox1" VerticalAlignment="Top" Width="233" AcceptsReturn="True" />
<TextBox Height="177" HorizontalAlignment="Left" Margin="258,12,0,0" Name="textBox2" VerticalAlignment="Top" Width="233" AcceptsReturn="True" />
<Button Content="Cut"
Height="23" HorizontalAlignment="Left" Margin="12,195,0,0" Name="button1" VerticalAlignment="Top" Width="75"
Command="ApplicationCommands.Cut"
CommandTarget="{Binding ElementName=textBox1}"/>
</Grid>
</Window>
如何让任何聚焦元素接收命令?
谢谢!