private void ReBuildContents() { contents.Children.Clear(); contents.RowDefinitions.Clear(); contents.ColumnDefinitions.Clear(); int sz = this.BoardSize; _cells = new SeaBoardCell[sz, sz]; contents.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(0, GridUnitType.Auto) }); contents.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(0, GridUnitType.Auto) }); for (int i = 0; i < sz; i++) { contents.RowDefinitions.Add(new RowDefinition()); contents.ColumnDefinitions.Add(new ColumnDefinition()); var hh = new UserControl() { Content = ((char)('A' + i)).ToString(), HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center }; contents.Children.Add(hh); Grid.SetRow(hh, 0); Grid.SetColumn(hh, i + 1); var vh = new UserControl() { Content = (i + 1).ToString(), HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center }; contents.Children.Add(vh); Grid.SetRow(vh, i + 1); Grid.SetColumn(vh, 0); } for (int y = 0; y < sz; y++) { for (int x = 0; x < sz; x++) { var cell = new SeaBoardCell(this, x, y); _cells[x, y] = cell; contents.Children.Add(cell.Control); Grid.SetRow(cell.Control, y + 1); Grid.SetColumn(cell.Control, x + 1); cell.Control.PreviewMouseDown += (s, e) => e.Handled = true; cell.Control.MouseUp += (s, e) => this.OnCellMouseUp(this, new SeaBoardCellEventArgs(cell, e)); cell.Control.MouseEnter += (s, e) => this.OnCellMouseEnter(this, new SeaBoardCellEventArgs(cell, e)); cell.Control.MouseLeave += (s, e) => this.OnCellMouseLeave(this, new SeaBoardCellEventArgs(cell, e)); } } }
public SeaBoardCellEventArgs(SeaBoardCell cell, MouseEventArgs mouseInfo) { this.Cell = cell; this.MouseInfo = mouseInfo; this.Button = (mouseInfo is MouseButtonEventArgs) ? (mouseInfo as MouseButtonEventArgs).ChangedButton : (MouseButton?)null; }