2)布局網格:
為了可以適應不同GridSize尺寸的需要,我們需要更加靈活的網格模型,因此選擇Grid做為主體,當加載完地圖後,我們可以根據該地圖的相關數據自動在畫板ScrollVIEwer中動態添加Grid(包括行數和列數):
……
grid = new Grid() {
ShowGridLines = ShowGrid.IsChecked.Value,
Width = width,
Height = height,
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
};
GridWidth.Text = gridWidth.ToString();
GridHeight.Text = gridHeight.ToString();
for (int x = 0; x < grid.Width / gridWidth; x++) {
ColumnDefinition col = new ColumnDefinition() {
Width = new GridLength(gridWidth),
};
grid.ColumnDefinitions.Add(col);
}
for (int y = 0; y < grid.Height / gridHeight; y++) {
RowDefinition row = new RowDefinition() {
Height = new GridLength(gridHeight),
};
grid.RowDefinitions.Add(row);
}
scrollVIEwer.Content = grid;
……
Grid的ShowGridLines參數非常有意思,通過將之設置為True即可顯示出Grid所有單元格的邊框:
遺憾的是,一旦啟動了網格邊框顯示,將嚴重影響界面線程的性能,仿佛有些雞肋了,有時間我還打算嘗試其他的方式來高效的設置單元格邊框。