【问题标题】:C# WPF Array of Buttons, Button CoordinatesC# WPF 按钮数组,按钮坐标
【发布时间】:2013-03-22 14:48:48
【问题描述】:

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

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

    }

【问题讨论】:

  • 在 btn_Click() 中你可以这样做: Button btn = (Button)sender;
  • @Dilshod 是的,但为了安全起见Button btn = sender as Button;,如果发送者没有按钮,将返回null,而不是抛出异常。
  • 你是对的。或者你可以先检查:if(sender is Button){//then cast it}
  • 如果我这样做,我的坐标会在哪里?
  • @bash.d 为什么要设置另一个控件来触发btn_Click?如果这样做了,我会希望程序崩溃,以便我可以立即修复它!

标签: c# button wpf-controls


【解决方案1】:

我只是想了一个疯狂的想法...为什么不自己创建Button 并称它为MazeButton 或其他什么? 从Button派生并添加一些属性,利用继承。

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

//rest of implementation

}

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

【讨论】:

    【解决方案2】:

    试试这个:

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

    【讨论】:

    • 您在这里进行了两次强制转换,第一次检查发件人是否是按钮,然后在将其强制转换为按钮时再次检查。您可以使用 as 并检查 null,从而产生一次强制转换。
    • 几乎。虽然这不会编译:)(您忘记了as Button1 之后的;Button1 可能应该是Button)。如果else 案例的唯一目的是引发异常,那么您不妨执行Button btn = (Button)sender;,如果发件人不是Button,它将引发异常——这是我更喜欢的。但其他人可能更喜欢as Button
    • 我认为使用它的人可以修复它。我没有尝试编译它。:)
    【解决方案3】:

    这是问题的另一个方法。我将每个按钮存储在元组列表而不是数组中,然后我搜索包含 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
            }
       }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-05
      • 2013-07-11
      • 1970-01-01
      相关资源
      最近更新 更多