Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
957 views
in Technique[技术] by (71.8m points)

wpf - Do I have to use CommandTarget? I thought any focused element would receive the Command

I'm trying to understand how to use RoutedCommands. I was under the impression that if I don't specify a CommandTarget on the Button, any focused element will receive the command. But for some reason it doesn't work. Here is the xaml that doesn't work:

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

If I add CommandTarget to the Button it works, but only for the textbox that is specified of course.

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

How can I make any focused element receive the command?

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You must set FocusManager.IsFocusScope to True.

<Button Content="Cut"  FocusManager.IsFocusScope="True"         
        Margin="12,195,0,0" 
        Height="23" HorizontalAlignment="Left" Name="button1" VerticalAlignment="Top" Width="75"                     
        Command="ApplicationCommands.Cut"/>

According to http://msdn.microsoft.com/en-us/magazine/cc785480.aspx, the reason for that is this:

If IsFocusScope="False", the command invoker looks for a command binding between its own location in the visual tree and the root of the visual tree.

If IsFocusScope="True", the command invoker also looks along the visual tree path from the root to the focus element for a command binding.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...