How to create and use matrix of (color) boxes C# WPF

匿名 (未验证) 提交于 2019-12-03 02:03:01

问题:

I have to do some sort of game with WPF App that contain some matrix of color boxes (ex. 10x10). On-click at some it must eliminate itself and surrounding boxes with the same color if there are more than 3, and after elimination these boxes grant some random color.

I'm fairly new in WPF apps, but I have some knowledge of C# Programming and I can't figure out from where I should start. Most difficult part for me is "spawning" this boxes and use it like a matrix.

So far I found some project that I thought it will help me, but not really.

Can someone navigate from where I can start and which is most relevant way to do this.

Thank you.

回答1:

ItemsControl + UniformGrid as a Panel is a good choice to display a matrix

view

code-behind

public partial class MainWindow : Window {     List _board;     public MainWindow()     {         InitializeComponent();         int rows = 10;         int columns = 10;         _board = new List();         for(int r = 0; r

you can create and use more complex type instead of Point and improve ItemTemplate to continue development. current ItemTemplate is nothing more that a rectangle

I used code-behind for demonstration, but in wpf MVVM in a preferred approach


EDIT extended example

in most cases you don't have to work with UI elements directly

to support different Colors I will create a custom class

public class MatrixElement {     private string _color;      public MatrixElement(int x, int y)     {         X = x;         Y = y;     }      public int X { get; private set; }     public int Y { get; private set; }      public string Color     {         get { return _color; }         set         {             _color = value;             if (ColorChanged != null)                 ColorChanged(this, EventArgs.Empty);         }     }      public event EventHandler ColorChanged; } 

window code has changed accordingly

List _board; public MainWindow() {     InitializeComponent();     int rows = 10;     int columns = 10;     _board = new List();     for (int r = 0; r 

ItemTemplate was modified a bit



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!