【问题标题】:Wpf Event in XAML cannot get the right focus on buttonXAML 中的 Wpf 事件无法正确关注按钮
【发布时间】:2013-10-17 07:30:42
【问题描述】:

当我按下键盘上的箭头键时,我尝试让按钮移动。 但是我得到的是我总是需要先用鼠标按下按钮才能获得正确的焦点,然后我可以用左箭头键移动它,否则不行。但是,据我所知,KeyDown 事件是由 Grid 而不是按钮触发的。

这是我在后面的代码中的做法:

private void Panel_KeyDown(object sender, KeyEventArgs e)
 {
    Button source = Baffle;
     if (source != null)
     {
        if (e.Key == Key.Left)
          {
             source.Margin = new Thickness(source.Margin.Left - 1, source.Margin.Top,
             source.Margin.Right + 1, source.Margin.Bottom);
            }
        }
 }

XAML:

<Grid Name="Panel" KeyDown="Panel_KeyDown"  Background="BlanchedAlmond">
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Button Name="Baffle" Template="{StaticResource ButtonTemplate}"   
Grid.Row="1" VerticalAlignment="Bottom" Margin="20" HorizontalAlignment="Center" 
Width="50" Height="20"/>
</Grid>

谁能解释一下?谢谢。

【问题讨论】:

    标签: c# wpf xaml event-handling windows-controls


    【解决方案1】:

    有趣...不知道为什么,但如果你想以一种简单的方式解决它,你可以使用这个:

    public partial class MainWindow : Window
    {
        private Button source;
        public MainWindow()
        {
            InitializeComponent();
            source = Baffle;
            source.Focus();
        }
    
        private void Panel_KeyDown(object sender, KeyEventArgs e)
        {
            if (source != null)
            {
                if (e.Key == Key.Left)
                {
                    source.Margin = new Thickness(source.Margin.Left - 1, source.Margin.Top,
                    source.Margin.Right + 1, source.Margin.Bottom);
                }
            }
        }
    }
    

    (只需将该按钮的焦点放在加载上,您就可以将其移动到您的心脏内容)。

    【讨论】:

      【解决方案2】:

      没错 - 只有当 Grid(Panel) 关注它时,您的 KEYDOWN 事件才会触发。但是当您的应用程序启动时,它并没有关注它,只有当您选择 Grid 上的任何控件(例如此按钮或另一个按钮)时才会获得它。 MainWindow 专注于启动,因此只需将此事件处理程序添加到 MainWindow KeyDown。

       <Window x:Class="WpfApplication4.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" KeyDown="Panel_KeyDown">
          <Grid Name="Panel"   Background="BlanchedAlmond">
          .....
      

      【讨论】:

        【解决方案3】:

        这是因为默认情况下Grid 是不可聚焦的,所以KeyEventGrid 具有焦点或Grid FocusScope 中的控件之一具有逻辑焦点之前将无法工作。

        您可以将Grid 设置为Focusable 并使用FocusManager 将FocusedElement 设置为网格,这将起作用

        例子:

        <Grid Name="Panel" KeyDown="Panel_KeyDown"  Background="BlanchedAlmond" FocusManager.FocusedElement="{Binding ElementName=Panel}" Focusable="True">
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <Button Name="Baffle"    
        Grid.Row="1" VerticalAlignment="Bottom" Margin="20" HorizontalAlignment="Center" 
        Width="50" Height="20"/>
            </Grid>
        

        【讨论】:

          猜你喜欢
          • 2016-06-11
          • 2021-05-15
          • 1970-01-01
          • 2015-04-23
          • 2013-05-06
          • 2015-02-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多