1

私は独自のカスタム キャンバスを作成しようとして、小さな四角形で構成される小さな迷路を描きたいと考えました。私の問題は、画面上に4つの長方形ではなく4つの小さな点が表示されることです(2 X 2フィールドで試した場合)。ここにいくつかのコードがあります:

public class LabyrinthCanvas : System.Windows.Controls.Canvas
{
    public static readonly int RectRadius = 60;

    public ObservableCollection<ObservableCollection<Rect>> Rectangles;

    public LabyrinthCanvas()
    {
        Rectangles = new ObservableCollection<ObservableCollection<Rect>>();
    }

    public void AddRectangles(int Height, int Width)
    {
        for (int iHeight = 0; iHeight < Height; iHeight++)
        {
            ObservableCollection<Rect> newRects = new ObservableCollection<Rect>();
            newRects.CollectionChanged += RectanglesChanged;
            Rectangles.Add(newRects);

            for (int iWidth = 0; iWidth < Width; iWidth++)
            {
                Rect rect = new Rect(iHeight * RectRadius, iWidth * RectRadius);
                Rectangles[iHeight].Add(rect);
            }
        }
    }

    public void RectanglesChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
        {

            foreach (object rect in e.NewItems)
            {
                if (rect is Rect)
                {
                    this.Children.Add(((Rect)rect).innerRectangle);
                    System.Windows.Controls.Canvas.SetTop(((Rect)rect).innerRectangle, ((Rect)rect).YPos);
                    System.Windows.Controls.Canvas.SetLeft(((Rect)rect).innerRectangle, ((Rect)rect).XPos);
                }
            }
        }
        else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove)
        {
            foreach (Rect rect in e.OldItems)
            {
                this.Children.Remove(rect.innerRectangle);
            }
        } 
    }
}

public class Rect : INotifyPropertyChanged
{
    public Rect(int YPos, int XPos)
    {
        innerRectangle.Stroke = System.Windows.Media.Brushes.Black;
        innerRectangle.Fill = System.Windows.Media.Brushes.Blue;
        this.YPos = YPos;
        this.XPos = XPos;
    }

    public System.Windows.Shapes.Rectangle innerRectangle = new System.Windows.Shapes.Rectangle();

    public int YPos;
    public int XPos;
}

重要なことは次のことだと思います。

this.Children.Add(((Rect)rect).innerRectangle);
                    System.Windows.Controls.Canvas.SetTop(((Rect)rect).innerRectangle, ((Rect)rect).YPos);
                    System.Windows.Controls.Canvas.SetLeft(((Rect)rect).innerRectangle, ((Rect)rect).XPos);

表示されているコードから削除した追加のプロパティが必要で、Rectangle から継承できないため、独自のクラス「Rect」を使用しています。

4

1 に答える 1

1

あなたが最終結果をどのように見せたいか完全にはわからないので、あなたが求めている正確な解決策を提案することはおそらくできないでしょう.

つまり、長方形ではなく画面上の小さな点を取得している理由は、キャンバスが指定された座標でオブジェクトをレンダリングしているためinnerRectangleですRectが、その寸法の設定を初期化していないためですinnerRectangle

表示されているドットは、幅/高さのない長方形で、Blackストロークがレンダリングされています (ドット)。

次の行に沿って何かを試すと、何が起こっているかがわかります。

    public System.Windows.Shapes.Rectangle innerRectangle = new System.Windows.Shapes.Rectangle() { Width = 10, Height = 10 }; 
于 2013-07-27T16:19:43.447 に答える