/// <summary>
        /// Highlights a box; this function is passed to the user's code as a delegate.
        /// </summary>
        /// <param name="box">The box to highlight.</param>
        /// <param name="color">The colour to use when highlighting the box.</param>
        public void HighlightBox(Box box, string color)
        {
            // Make sure this is executed on the UI thread
            this.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(delegate
            {
                if (box == null)
                {
                    return;
                }

                try
                {
                    box.Highlighted = true;

                    if (!String.IsNullOrEmpty(color.Trim()))
                    {
                        Brush highlightColor = Utilities.BrushFromString(color);
                        box.HighlightColor = highlightColor;
                    }
                }
                catch (Exception e)
                {
                    this.Exception(e.ToString());
                    return;
                }
            }));

            if (this.stopExecuting)
            {
                Thread.CurrentThread.Abort();
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Populates the grid with boxes.
        /// </summary>
        /// <param name="constraint">The amount of space available to layout in.</param>
        /// <returns>The amount of space used for layout.</returns>
        protected override System.Windows.Size MeasureOverride(System.Windows.Size constraint)
        {
            int numRows = (int)(constraint.Height / BoxSize);
            int numColumns = (int)(constraint.Width / BoxSize);
            int capacity = numRows * numColumns;

            // Regenerate the grid if it can hold a different amount of items
            if (capacity != this.oldCapacity)
            {
                if (capacity == 0)
                {
                    this.Children.Clear();
                    this.ColumnDefinitions.Clear();
                    this.RowDefinitions.Clear();
                }
                else
                {
                    // Adjust number of rows
                    if (this.RowDefinitions.Count <= numRows)
                    {
                        for (int i = this.RowDefinitions.Count; i < numRows; i++)
                        {
                            this.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
                        }
                    }
                    else
                    {
                        this.RowDefinitions.RemoveRange(numRows, this.RowDefinitions.Count - numRows);
                    }

                    // Adjust number of columns
                    if (this.ColumnDefinitions.Count <= numColumns)
                    {
                        for (int i = this.ColumnDefinitions.Count; i < numColumns; i++)
                        {
                            this.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });
                        }
                    }
                    else
                    {
                        this.ColumnDefinitions.RemoveRange(numColumns, this.ColumnDefinitions.Count - numColumns);
                    }

                    // Adjust number of children
                    if (capacity < this.oldCapacity)
                    {
                        this.Children.RemoveRange(capacity, this.oldCapacity - capacity);
                    }

                    // Re-index children
                    int childIndex = 0;
                    for (int i = 0; i < numRows; i++)
                    {
                        for (int j = 0; j < numColumns; j++)
                        {
                            Box square;

                            if (childIndex >= this.oldCapacity)
                            {
                                square = new Box();
                                this.Children.Add(square);
                            }
                            else
                            {
                                square = (Box)this.Children[childIndex];
                            }

                            SetRow(square, i);
                            SetColumn(square, j);
                            square.Number = childIndex;

                            childIndex++;
                        }
                    }
                }
            }

            this.oldCapacity = capacity;
            this.oldRows = numRows;
            this.oldColumns = numColumns;

            return base.MeasureOverride(constraint);
        }