具有水平和垂直网格线且目标居中的 WPF 数据网格

本文关键字:WPF 数据 网格 数据网 目标 水平 垂直 网格线 | 更新日期: 2023-09-27 18:34:57

我创建了一个空网格,其中包含水平线和垂直线,但没有数据。 在这个网格的中心,我想要一个可以用箭头键移动的加号光标,我没有找到任何这样做的好例子......

这是我到目前为止的WPF代码,但我不清楚如何在此网格的中间添加一个+号,我可以向上/向下/向左/向右移动。

非常感谢任何建议,因为我似乎在这里遇到了障碍。

<Window x:Class="Crosshairs.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>
<Border BorderBrush="Black" BorderThickness="2" VerticalAlignment="Center" HorizontalAlignment="Center" >
  <Grid ShowGridLines="True" Width="250" Height="250">
    <Grid.ColumnDefinitions>
      <ColumnDefinition />
      <ColumnDefinition />
      <ColumnDefinition />
      <ColumnDefinition />
      <ColumnDefinition />
      <ColumnDefinition />
      <ColumnDefinition />
      <ColumnDefinition />
      <ColumnDefinition />
      <ColumnDefinition />
      <ColumnDefinition />
      <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
      <RowDefinition />
      <RowDefinition />
      <RowDefinition />
      <RowDefinition />
      <RowDefinition />
      <RowDefinition />
      <RowDefinition />
      <RowDefinition />
      <RowDefinition />
      <RowDefinition />
      <RowDefinition />
      <RowDefinition />
      <RowDefinition />
      <RowDefinition />
      <RowDefinition />
      <RowDefinition />
    </Grid.RowDefinitions>
  </Grid>
  </Border>
  </Grid>
  </Window>

具有水平和垂直网格线且目标居中的 WPF 数据网格

这个

怎么样?

  <Window x:Class="Crosshairs.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>
    <Border BorderBrush="Black" BorderThickness="1" VerticalAlignment="Center" HorizontalAlignment="Center" >
  <Grid x:Name="PART_Grid" ShowGridLines="True" Width="250" Height="250" DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}">
    <Grid.ColumnDefinitions>
      <ColumnDefinition />
      <ColumnDefinition />
      <ColumnDefinition />
      <ColumnDefinition />
      <ColumnDefinition />
      <ColumnDefinition />
      <ColumnDefinition />
      <ColumnDefinition />
      <ColumnDefinition />
      <ColumnDefinition />
      <ColumnDefinition />
      <ColumnDefinition />
      <ColumnDefinition />
      <ColumnDefinition />
      <ColumnDefinition />
      <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
      <RowDefinition />
      <RowDefinition />
      <RowDefinition />
      <RowDefinition />
      <RowDefinition />
      <RowDefinition />
      <RowDefinition />
      <RowDefinition />
      <RowDefinition />
      <RowDefinition />
      <RowDefinition />
      <RowDefinition />
      <RowDefinition />
      <RowDefinition />
      <RowDefinition />
      <RowDefinition />
      <RowDefinition />
      <RowDefinition />
      <RowDefinition />
      <RowDefinition />
    </Grid.RowDefinitions>
    <TextBlock Text="+" FontSize="24" Grid.Column="{Binding Position.X}" Grid.Row="{Binding Position.Y}" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="Red" Margin="-17,-20,0,-27" />
  </Grid>
</Border>

代码背后

public partial class MainWindow : Window
{
    public Point Position
    {
        get { return (Point)GetValue(PositionProperty); }
        set { SetValue(PositionProperty, value); }
    }
    // Using a DependencyProperty as the backing store for Position.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty PositionProperty =
        DependencyProperty.Register("Position", typeof(Point), typeof(MainWindow), new PropertyMetadata(new Point()));

    public MainWindow()
    {
        InitializeComponent();
    }

    protected override void OnKeyDown(KeyEventArgs e)
    {
        switch(e.Key)
        {
            case Key.Up:
                if (this.Position.Y <= 0) { break; }
                this.Position = new Point(this.Position.X, this.Position.Y - 1); break;
            case Key.Down:
                if (this.Position.Y >= this.PART_Grid.RowDefinitions.Count - 1) { break; }
                this.Position = new Point(this.Position.X, this.Position.Y + 1); break;
            case Key.Left:
                if (this.Position.X <= 0) { break; }
                this.Position = new Point(this.Position.X - 1, this.Position.Y); break;
            case Key.Right:
                if (this.Position.X >= this.PART_Grid.ColumnDefinitions.Count - 1) { break; }
                this.Position = new Point(this.Position.X + 1, this.Position.Y); break;
        }
        base.OnKeyDown(e);
    }
    protected override void OnInitialized(EventArgs e) 
    { 
        this.Position = new Point(8, 9); 
        // set the position 
        base.OnInitialized(e); 
    }
}