0

我的任务是编写一个简单的 Lee 算法演示程序,用于在迷宫中寻路。我想让它有点图形交互:创建一个具有可变数量的行和列的 2D 表,可以单击其单元格(并且应该能够跟踪单击的单元格的位置)。这是因为我想让用户绘制迷宫障碍物、设置起点等。可以帮助我做到这一点的最佳图形组件是什么,我如何与它的单元格交互?

4

1 回答 1

2

根据您的评论,我会说 WPF 是这项任务的更自然的候选者,因为它被设计用于执行自定义布局的东西。这是一个代码示例,我使用带有统一网格的项目控件组合在一起,该网格显示单元格网格 - 单击单元格将其选中,再次单击将取消选中它。

这不是很好的 WPF,但它可能会给您一些想法并帮助您入门。

应用:

应用示例图片

XAML:

<Window x:Class="LeeAlgorithm.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="380" Width="350">
    <Grid>
        <ItemsControl ItemsSource="{Binding Cells}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <UniformGrid Columns="5" />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <DataTemplate.Resources>
                        <Style TargetType="TextBlock">
                            <Setter Property="FontSize" Value="18"/>
                            <Setter Property="HorizontalAlignment" Value="Center"/>


        </Style>
                </DataTemplate.Resources>
                <Grid Width="30" Height="30" Margin="2">
                    <DockPanel ZIndex="9999">
                        <!-- This is a hack to capture the mouse click in the area of the item -->
                        <Rectangle 
                            Fill="Transparent" 
                            DockPanel.Dock="Top" 
                            MouseDown="UIElement_OnMouseDown" 
                            Tag="{Binding}" />
                    </DockPanel>
                    <Grid>
                    <Rectangle Fill="{Binding Path=Color}" Stroke="Red" StrokeDashArray="1 2" />
                    <TextBlock Margin="3,3,3,0" Text="{Binding Path=CellNumber}"/>
                    </Grid>
                </Grid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Grid>

后面的代码(您的 MainWindow.xaml.cs 代码):

namespace LeeAlgorithm
{
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Shapes;

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            this.Cells = new ObservableCollection<Cell>();

            for (int i = 0; i != 50; ++i)
            {
                this.Cells.Add(new Cell { CellNumber = i });
            }

            InitializeComponent();

            DataContext = this;
        }

        public ObservableCollection<Cell> Cells { get; private set; }

        private void UIElement_OnMouseDown(object sender, MouseButtonEventArgs e)
        {
            var cell = (Cell)((Rectangle)sender).Tag;

            if (!cell.IsSelected)
            {
                cell.Color = new SolidColorBrush(Colors.HotPink);
                cell.IsSelected = true;
            }
            else
            {
                cell.Color = new SolidColorBrush(Colors.Silver);
                cell.IsSelected = false;
            }
        }
    }

    public class Cell : INotifyPropertyChanged
    {
        private int cellNumber;

        private SolidColorBrush color = new SolidColorBrush(Colors.Silver);

        public event PropertyChangedEventHandler PropertyChanged;

        public int CellNumber
        {
            get
            {
                return this.cellNumber;
            }
            set
            {
                if (value == this.cellNumber)
                {
                    return;
                }
                this.cellNumber = value;
                this.OnPropertyChanged("CellNumber");
            }
        }

        public SolidColorBrush Color
        {
            get
            {
                return this.color;
            }
            set
            {
                if (Equals(value, this.color))
                {
                    return;
                }
                this.color = value;
                this.OnPropertyChanged("Color");
            }
        }

        public bool IsSelected { get; set; }

        protected virtual void OnPropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

快乐编码!

于 2013-02-27T12:48:19.267 回答