WPF Datagrid,带有居中目标的水平和垂直网格线

时间:2014-12-23 00:38:48

标签: c# wpf grid

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

这是我目前为止的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>

1 个答案:

答案 0 :(得分:1)

这个怎么样?

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