I thought it was about time I learned about WPF so thought I’d start with a very simple application, a grid showing the ASCII characters. It’s kind of pointless at the moment since you can find this information on the web pretty easily but I think I can make it somewhat more powerful and in the process learn more about WPF. This is what I’d like to add in the future and will hopefully blog about as I implement them
- Turn the grid into a control
- Show the extended ASCII characters
- Make it look much sexier
- Let the user choose the font
- Let the user change the code page
The code currently looks like this. It’s all pretty straighforward. The first thing to note is the GridUnitType.Star enumerated value which means each column and row will be spaced equally in the window.
The other thing to note is the weird way the text blocks are assigned to the correct cells of the grid. It doesn’t seem a natural way to do it, but I guess once you know that’s how it’s done, it’s pretty straightforward.
public AsciiWindow() { InitializeComponent(); BindGrid(); } private void BindGrid() { const int width = 16; const int height = 16; for (int x = 0; x < width; x++) { m_Grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star), }); } for (int y = 0; y < height; y++) { m_Grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star), }); } for (int x = 0; x < width; x++) { for (int y = 0; y < height/2; y++) { int asciiValue = ((y*width)+(x+1)); // number TextBlock rect = new TextBlock(); rect.Text = asciiValue.ToString(); rect.SetValue(Grid.RowProperty, y*2); rect.SetValue(Grid.ColumnProperty, x); m_Grid.Children.Add(rect); // ASCII value rect = new TextBlock(); rect.Text = Convert.ToChar(asciiValue).ToString(); rect.SetValue(Grid.RowProperty, (y * 2)+1); rect.SetValue(Grid.ColumnProperty, x); m_Grid.Children.Add(rect); } } }
No comments:
Post a Comment