简体   繁体   中英

WPF Datagrid with horizontal and vertical grid lines with centered target

I have created an empty grid with both horizontal and vertical lines but no data. Centered within this grid I would like a plus sign cursor that can be moved with the arrow keys, I am not finding any good examples of doing this....

Here is my WPF code that I have so far but I am unclear how to add a + sign in the middle of this grid that I can move up/down/left/right.

Any advice is much appreciated as I am seeming to hit a roadblock here.

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

How about this one?

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

behind code

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); 
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM