How can I get the Position of textbox that has been pressed?

后端 未结 2 1636
深忆病人
深忆病人 2020-11-29 14:01

I am writing in WPF the Sudoku Game and i make in run time 81 textboxes on canvas:

public partial class Test : Window
    {
        private TextBox[,] texts          


        
2条回答
  •  一向
    一向 (楼主)
    2020-11-29 14:55

    Use an attached event handler

    AddHandler(TextBox.KeyDownEvent, new RoutedEventHandler(MyFieldClick));
    

    this will get called for every text box inside the calling element. So if you place that in your window, every text box KeyDownEvent is patched through to the MyFieldClick event handler method. The sender is in this case the textbox that originally sended the event.

    But i aggree with HighCore, you should utilize WPF more. In your case i see ItemControls and a Grid or better an UniformGridPanel.

    Btw TextChanged might be the more appropriate event for your case.

提交回复
热议问题