How can I get the Position of textbox that has been pressed?
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 = new TextBox[9, 9];
GameBoard board = new GameBoard();
public Test(string path)
{
InitializeComponent();
Initialization_text();
}
void Initialization_text()
{
for (int i = 0; i < texts.GetLength(0); i++)
{
for (int j = 0; j < texts.GetLength(1); j++)
{
texts[i, j] = new TextBox();
texts[i, j].Name = "txt" + i + j;
texts[i, j].Width = 40;
texts[i, j].Height = 40;
texts[i, j].MaxLength = 1;
texts[i, j].FontSize = 22;
texts[i, j].FontWeight = FontWeights.Bold;
texts[i, j].Foreground = new SolidColorBrush(Colors.DimGray);
texts[i,j].TextAlignment = TextAlignment.Center;
Canvas.SetLeft(texts[i, j], (i+1)*40);
Canvas.SetTop(texts[i, j], (j+1)*40);
canvas1.Children.Add(texts[i, j]);
}
}
}
I need get the position of textbox that user is enter the number for checking the value, but i cant write the method that called TextBoxKeyDown because its make in run time
but if i write this method :
private void canvas1_KeyDown(object sender, KeyEventArgs e)
{
if (sender.GetType().Name == "TextBox")//the sender is canvas
{
}
}
how can i get the textbox that user is enter the number? pls help...
Solution 1:
Ok. Delete all your code and start all over.
This is how you do a Sudoku board in WPF:
XAML:
<Window x:Class="WpfApplication4.Window17"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window17" Height="300" Width="300">
<ItemsControl ItemsSource="{Binding}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Rows="9" Columns="9"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Grid.Row" Value="{Binding Row}"/>
<Setter Property="Grid.Column" Value="{Binding Column}"/>
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding Value}" VerticalAlignment="Stretch" FontSize="20" TextAlignment="Center"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Window>
Code Behind:
using System.Collections.Generic;
using System.Windows;
using System.ComponentModel;
namespace WpfApplication4
{
public partial class Window17 : Window
{
public Window17()
{
InitializeComponent();
var random = new Random();
var board = new List<SudokuViewModel>();
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
board.Add(new SudokuViewModel() {Row = i, Column = j,Value = random.Next(1,20)});
}
}
DataContext = board;
}
}
}
ViewModel:
public class SudokuViewModel:INotifyPropertyChanged
{
public int Row { get; set; }
public int Column { get; set; }
private int _value;
public int Value
{
get { return _value; }
set
{
_value = value;
NotifyPropertyChange("Value");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChange(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
As you can see, Im in no way creating or manipulating UI elements in code. That's plain wrong in WPF. You must learn MVVM, and understand that UI is Not Data. Data is Data. UI is UI
Now, whenever you need to operate against the Value in the TextBoxes, just operate against the public int Value
property in the ViewModel. Your application logic and your UI must be completely decoupled.
Just copy and paste my code in a File -> New Project -> WPF Application
and see the results for yourself. This is how it looks in my computer:
Edit:
I have modified the sample to call a method when a Value is changed.
Please understand that you should NOT operate against the UI in WPF, but against DATA. What you really care about is DATA (The ViewModel), not the UI itself.
using System.Collections.Generic;
using System.Windows;
using System.ComponentModel;
using System;
namespace WpfApplication4
{
public partial class Window17 : Window
{
public List<SudokuViewModel> Board { get; set; }
public Window17()
{
InitializeComponent();
var random = new Random();
Board = new List<SudokuViewModel>();
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
Board.Add(new SudokuViewModel()
{
Row = i, Column = j,
Value = random.Next(1,20),
OnValueChanged = OnItemValueChanged
});
}
}
DataContext = Board;
}
private void OnItemValueChanged(SudokuViewModel vm)
{
MessageBox.Show("Value Changed!\n" + "Row: " + vm.Row + "\nColumn: " + vm.Column + "\nValue: " + vm.Value);
}
}
public class SudokuViewModel:INotifyPropertyChanged
{
public int Row { get; set; }
public int Column { get; set; }
private int _value;
public int Value
{
get { return _value; }
set
{
_value = value;
NotifyPropertyChange("Value");
if (OnValueChanged != null)
OnValueChanged(this);
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChange(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public Action<SudokuViewModel> OnValueChanged { get; set; }
}
}