2D grid with selectable items in c# Wforms?

后端 未结 1 490
情书的邮戳
情书的邮戳 2020-12-20 07:56

How can be done a 2D of 10x10 selectable elements each element with text in a windows form? Is there an easy way of doing this?

I need to select some elements in an

1条回答
  •  Happy的楠姐
    2020-12-20 08:25

    Posting this as an answer because the OP asked for it:

    
    
    
        
            
            
            
        
    
        
            
            
                
                    
                        
                            
                                
                                    
                                    
                                
                            
                        
                    
                
            
        
    
        
            
                
                    
                        
                    
                
                
                    
                        
                            
                        
                    
                
            
            
                
            
        
    
    
    

    Code Behind:

    public partial class GridRobot : Window
    {
        public GridRobot()
        {
            InitializeComponent();
            DataContext = new GridRobotViewModel();
        }
    }
    

    View Model:

    public class GridRobotViewModel: PropertyChangedBase
    {
        private int _size;
        public int Size
        {
            get { return _size; }
            set
            {
                _size = value;
                OnPropertyChanged("Size");
                CreateItems();
            }
        }
    
        private ObservableCollection _squares;
        public ObservableCollection Squares
        {
            get { return _squares ?? (_squares = new ObservableCollection()); }
        }
    
        private ObservableCollection _route;
        public ObservableCollection Route
        {
            get { return _route ?? (_route = new ObservableCollection()); }
        }
    
        private void CreateItems()
        {
            Squares.Clear();
            Route.Clear();
            for (int i = 0; i < Size; i++)
            {
                for (int j = 0; j < Size; j++)
                {
                    Squares.Add(new GridItem() {Row = i, Column = j});
                }
            }
        }
    
        private Command _goToCommand;
        public Command GoToCommand
        {
            get { return _goToCommand ?? (_goToCommand = new Command(Goto){IsEnabled = true}); }
        }
    
        private void Goto(GridItem item)
        {
            if (item.PathIndex == null)
            {
                item.PathIndex = Squares.Max(x => x.PathIndex ?? 0) + 1;
    
                Route.Add(item);    
            }
        }
    }
    

    Data Item:

    public class GridItem: PropertyChangedBase
    {
        public int Row { get; set; }
    
        public int Column { get; set; }
    
        private int? _pathIndex;
        public int? PathIndex
        {
            get { return _pathIndex; }
            set
            {
                _pathIndex = value;
                OnPropertyChanged("PathIndex");
            }
        }
    }
    

    Support Classes for MVVM:

    public class PropertyChangedBase:INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
    
        protected virtual void OnPropertyChanged(string propertyName)
        {
            Application.Current.Dispatcher.BeginInvoke((Action) (() =>
                {
                    PropertyChangedEventHandler handler = PropertyChanged;
                    if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
                }));
        }
    }
    
    public class Command: ICommand
    {
        public Action Action { get; set; }
    
        public void Execute(object parameter)
        {
            if (Action != null && parameter is T)
                Action((T)parameter);
        }
    
        public bool CanExecute(object parameter)
        {
            return IsEnabled;
        }
    
        private bool _isEnabled;
        public bool IsEnabled
        {
            get { return _isEnabled; }
            set
            {
                _isEnabled = value;
                if (CanExecuteChanged != null)
                    CanExecuteChanged(this, EventArgs.Empty);
            }
        }
    
        public event EventHandler CanExecuteChanged;
    
        public Command(Action action)
        {
            Action = action;
        }
    }
    

    Result:

    enter image description here

    • Just copy and paste my code in a File -> New Project -> WPF Application and see the results for yourself.
    • You said 10 x 10, but I went a step further and added a Slider to make the grid size customizable.
    • Clicking on any cell will make it be queued as part of the Route.
    • Fully Resolution Independent.
    • I was about to start putting some really nice stuff on it (animations, robot movement represented by an ellipse, Lines for the Path, etc).
    • Forget winforms, it's useless.

    0 讨论(0)
提交回复
热议问题