2013-03-22 120 views
0

我在窗口的主网格中创建了一个按钮矩阵,并且还为每个按钮创建了一个事件。 我也有一个整数矩阵,它包含每个按钮的一些属性(例如int a [1,2]是按钮btn [1,2]的属性)。 我打算创建一个像迷宫一样的东西,在这个迷宫中,你只能通过骑士(从国际象棋)的方式跨越一个按钮到另一个。我不知道如何找到按钮的坐标,以便我可以更改当前按钮的位置。C#WPF按钮,按钮坐标阵列

Button[,] btn = new Button[25, 25]; 
     for (x = 5; x <= n; x++) 
     { 
      for (y = 5; y <= n; y++) 
      { 
       btn[x, y] = new Button(); 
       left += 72; 
       btn[x,y].Margin =new Thickness(left,top,0,0); 
       btn[x,y].Height = 32; 
       btn[x,y].Width = 32; 
       btn[x, y].Click += new RoutedEventHandler(btn_Click); 

       if (a[x, y] == 2) 
        btn[x,y].Background = Brushes.Red; 
       else 
        btn[x,y].Background = Brushes.Blue; 
       main.Children.Add(btn[x, y]); 


      } 
      left = 0; 
      top += 72; 
     } 

    } 
    private void btn_Click(object sender, RoutedEventArgs e) 
    { 

    } 
+1

in btn_Click()你可以这样做:Button btn =(Button)sender; – Dilshod 2013-03-22 14:52:19

+0

@Dilshod真,但要安全'Button btn = sender as Button;',如果发件人没有按钮,则返回'null'而不是抛出异常。 – 2013-03-22 14:58:02

+1

当然你是对的。或者你可以先检查:如果(发件人是按钮){//然后投它} – Dilshod 2013-03-22 14:59:18

回答

0

我只是去一个疯狂的想法...为什么不自己做Button并调用它MazeButton,还是什么? 从Button派生并添加一些属性,利用继承。

public class MazeButton : System.Windows.Controls.Button { 
private int left; 
private int top; 

//rest of implementation 

} 

通过这种方式,您可以将迷宫中的按钮所在的信息直接传递到按钮中。您可以定义自定义事件和其他任何你想要的东西。

0

试试这个:

private void btn_Click(object sender, RoutedEventArgs e) 
    { 

     Button btn = sender as Buttonl; 
     if(btn!=null) 
     { 
      Point renderedLocation = btn.TranslatePoint(new Point(0, 0), this); 
     } 
     else 
     { 
       //you may throw an exception if you want. 
     } 
    } 
+0

你在这里做了两个演员,第一个演员是检查发件人是否是Button,然后再将它转换为按钮。您可以使用as并检查null,从而导致一次强制转换。 – Default 2013-03-22 15:15:42

+0

我希望这会足够好。差不多是 – Dilshod 2013-03-22 15:20:52

+0

。这虽然不能编译:)(你忘了';'在Button1之后''Button1'可能应该是'Button')。如果'else'的唯一目的是抛出一个异常,那么你可以做'Button btn =(Button)sender;'如果sender不是'Button'就会抛出一个异常 - 这就是我所希望的。但其他人可能更喜欢'按钮'。 – Default 2013-03-22 15:26:00

0

这是问题的另一个计算策略。我将每个按钮都存储在一个Tuple列表中,而不是一个数组,然后我搜索那个元组包含Linq按钮。

这是假设你在.NET 4.如果不是,可以写成类Tuple(你可以在SO上找到它)。

private List<Tuple<Button, Int32, Int32>> listButton; 
    private void SetButtons() 
    { 
     // TODO define what is n, left, top 

     listButton = new List<Tuple<Button, int, int>>(); 

     for (int x = 5; x <= n; x++) 
     { 
      for (int y = 5; y <= n; y++) 
      { 
       Button btn = new Button(); 
       left += 72; 
       btn.Margin = new Thickness(left, top, 0, 0); 
       btn.Height = 32; 
       btn.Width = 32; 
       btn.Click += new RoutedEventHandler(btn_Click); 

       if (a[x, y] == 2) 
        btn.Background = Brushes.Red; 
       else 
        btn.Background = Brushes.Blue; 

       listButton.Add(new Tuple<Button, int, int>(btn, x, y)); 

       main.Children.Add(btn); 
      } 
      left = 0; 
      top += 72; 
     } 
    } 

    private void btn_Click(object sender, RoutedEventArgs e) 
    { 
     Button button = (Button)sender; 

     var tuple = listButton.Where(t => t.Item1 == button).FirstOrDefault(); 

     if (tuple != null) 
     { 
      Int32 x = tuple.Item2; 
      Int32 y = tuple.Item3; 

      // Do whay you want this x and y found 
     } 
    }