WPF实现简单的9宫格键盘移动方块

实现用电脑键盘上下左右实现方块的移动demo
xaml文件代码:

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" KeyDown="Window_KeyDown">
    <Grid x:Name="gridWindow" ShowGridLines="True">
        <Grid.ColumnDefinitions>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <Border Name="border1" Background="Yellow" Grid.Column="0" Grid.Row="0"></Border>
        <Border Name="border2" Background="Pink" Grid.Column="1" Grid.Row="0"></Border>
        <Border Name="border3" Background="Yellow" Grid.Column="2" Grid.Row="0"></Border>
        <Border Name="border4" Background="Yellow" Grid.Column="0" Grid.Row="1"></Border>
        <Border Name="border5" Background="Yellow" Grid.Column="1" Grid.Row="1"></Border>
        <Border Name="border6" Background="Yellow" Grid.Column="2" Grid.Row="1"></Border>
        <Border Name="border7" Background="Yellow" Grid.Column="0" Grid.Row="2"></Border>
        <Border Name="border8" Background="Yellow" Grid.Column="1" Grid.Row="2"></Border>
        <Border Name="border9" Background="Yellow" Grid.Column="2" Grid.Row="2"></Border>
    </Grid>
</Window>

后台代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp1
{
    /// 
    /// MainWindow.xaml 的交互逻辑
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            //获取Grid下的子元素
            UIElementCollection children = gridWindow.Children;
            Border border = null;
            for (int i = 0; i < children.Count; i++)
            {
                if (children[i] is Border&& ((children[i] as Border).Background as SolidColorBrush).Color.Equals(Colors.Pink)){
                    border = children[i] as Border;
                }
                (children[i] as Border).Background=new SolidColorBrush(Colors.Yellow);
            }
            string name = border.Name;
            int index = Convert.ToInt32(name.Replace("border", ""));
            if (e.Key.Equals(Key.Up))
            {
                index = index - 3 >= 1 ? index - 3 : index;
            }else if(e.Key.Equals(Key.Down))
            {
                index = index + 3 <= 9 ? index + 3 : index;
            }else if(e.Key.Equals(Key.Left))
            {
                index=index-1>=1?index-1 : index;
            }
            else if(e.Key.Equals(Key.Right))
            {
                index=index+1<=9 ? index+1 : index;
            }
            object control = gridWindow.FindName("border" + index);
            if(control != null)
            {
                (control as Border).Background = new SolidColorBrush(Colors.Pink);
            }
        }
    }
}

案例效果如下
WPF实现简单的9宫格键盘移动方块_第1张图片

你可能感兴趣的:(WPF学习,wpf)