Witam,
Na początek przedstawię pliki swojego projektu:
MainWindow.xaml
<Window x:Class="BoardWithMouseInteraction.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:BoardWithMouseInteraction"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="500" WindowStartupLocation="CenterScreen"
MouseLeftButtonDown="Window_MouseLeftButtonDown">
<Grid>
<Rectangle Height="300" Width="300" Stroke="Black" StrokeThickness="0.5"/>
<Grid Name="grid_1" Height="300" Width="300">
</Grid>
<Grid Name="grid_2" Height="300" Width="300">
<Line X1="10" X2="10" Y1="0" Y2="300" Stroke="BLUE" StrokeThickness="0.5"/>
<Line X1="20" X2="20" Y1="0" Y2="300" Stroke="BLUE" StrokeThickness=".5"/>
<Line X1="30" X2="30" Y1="0" Y2="300" Stroke="BLUE" StrokeThickness=".5"/>
<Line X1="40" X2="40" Y1="0" Y2="300" Stroke="BLUE" StrokeThickness=".5"/>
<Line X1="50" X2="50" Y1="0" Y2="300" Stroke="BLUE" StrokeThickness=".5"/>
<Line X1="60" X2="60" Y1="0" Y2="300" Stroke="BLUE" StrokeThickness=".5"/>
<Line X1="70" X2="70" Y1="0" Y2="300" Stroke="BLUE" StrokeThickness=".5"/>
<Line X1="80" X2="80" Y1="0" Y2="300" Stroke="BLUE" StrokeThickness=".5"/>
<Line X1="90" X2="90" Y1="0" Y2="300" Stroke="BLUE" StrokeThickness=".5"/>
<Line X1="100" X2="100" Y1="0" Y2="300" Stroke="BLUE" StrokeThickness=".5"/>
<Line X1="0" X2="300" Y1="10" Y2="10" Stroke="BLUE" StrokeThickness="0.5"/>
<Line X1="0" X2="300" Y1="20" Y2="20" Stroke="BLUE" StrokeThickness="0.5"/>
<Line X1="0" X2="300" Y1="30" Y2="30" Stroke="BLUE" StrokeThickness="0.5"/>
<Line X1="0" X2="300" Y1="40" Y2="40" Stroke="BLUE" StrokeThickness="0.5"/>
<Line X1="0" X2="300" Y1="50" Y2="50" Stroke="BLUE" StrokeThickness="0.5"/>
<Line X1="0" X2="300" Y1="60" Y2="60" Stroke="BLUE" StrokeThickness="0.5"/>
<Line X1="0" X2="300" Y1="70" Y2="70" Stroke="BLUE" StrokeThickness="0.5"/>
<Line X1="0" X2="300" Y1="80" Y2="80" Stroke="BLUE" StrokeThickness="0.5"/>
<Line X1="0" X2="300" Y1="90" Y2="90" Stroke="BLUE" StrokeThickness="0.5"/>
<Line X1="0" X2="300" Y1="100" Y2="100" Stroke="BLUE" StrokeThickness="0.5"/>
</Grid>
</Grid>
</Window>
Klasa structuralelements
class StructuralElement
{
public int X { get; set; }
public int Y { get; set; }
public Rectangle Rect { get; private set; }
public StructuralElement(int x, int y)
{
X = x;
Y = y;
Rect = new Rectangle();
Rect.Width = Rect.Height = 10;
Rect.Fill = Brushes.Red;
}
}
klasa komórki
class Cell
{
public List<StructuralElement> myField { get; private set; }
public Cell()
{
myField = new List<StructuralElement>();
myField.Add(new StructuralElement(0, 0));
myField.Add(new StructuralElement(0, 5));
}
public Cell(int x, int y) // konstruktor do obsługi myszki
{
myField = new List<StructuralElement>();
SetShip(x, y);
}
public void RedrawCells()
{
foreach(StructuralElement tmp in myField)
{
Grid.SetColumn(tmp.Rect, tmp.X);
Grid.SetRow(tmp.Rect, tmp.Y);
tmp.Rect.Fill = System.Windows.Media.Brushes.Red;
}
}
public void SetShip(int x, int y) //metoda do obsługi myszki
{
myField.Add(new StructuralElement(x, y));
}
//###
}
oraz klasa główna
{
private Cell _cell;
private static readonly int SIZE = 10;
Point p;
int _mousePosition_x;
int _mousePosition_y;
public MainWindow()
{
InitializeComponent();
InitBoard();
InitCell();
}
void InitBoard()
{
for(int i=0; i < grid_1.Width / SIZE; i++)
{
ColumnDefinition columnDefinitions = new ColumnDefinition();
columnDefinitions.Width = new GridLength(SIZE);
grid_1.ColumnDefinitions.Add(columnDefinitions);
}
for(int j=0; j < grid_1.Height / SIZE; j++)
{
RowDefinition rowDefinition = new RowDefinition();
rowDefinition.Height = new GridLength(SIZE);
grid_1.RowDefinitions.Add(rowDefinition);
}
_cell = new Cell();
}
void InitCell()
{
foreach (StructuralElement tmp in _cell.myField)
{
grid_1.Children.Add(tmp.Rect);
}
_cell.RedrawCells();
}
private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if(e.LeftButton == MouseButtonState.Pressed)
{
p = e.MouseDevice.GetPosition(grid_1); // p ={x;y}
_mousePosition_x = Convert.ToInt32(p.X / SIZE);
_mousePosition_y = Convert.ToInt32(p.Y / SIZE);
_cell = new Cell(_mousePosition_x, _mousePosition_y);
}
}
//##
}
Efekt powyższych linijek w załączniku.
Program rysuje kratownicę (np do gry w statki lub do symulacji gry w życie). Potrafię na niej umieścić komórki w sposób "statyczny" - czyli na sztywno wprowadzenie współrzędnych w klasie Cell. Usiłuję tego dokonać poprzez kliknięcie w komórkę myszką. Zdołałem określić współrzędne komórki na którą klikam. Posiadam zmienne które należy podstawić do wypełnienia(_mousepositionx oraz _mousepositiony w klasie MainWindow). Proszę o wskazówkę jak mogę to rozwiązać. Dłubię i dochodzę do wniosku, że jestem już dosyć blisko :) Z góry dziękuję chętnym za pomoc.