Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9085337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:13:14+00:00 2026-06-16T21:13:14+00:00

I have a custom canvas that’s supposed to fill itself with the maximum amount

  • 0

I have a custom canvas that’s supposed to fill itself with the maximum amount of Tile objects.

If it’s resized to be bigger, it fills the empty space with additional Tiles.
If It’s resized to be smaller, it removed the Tiles that are no longer visible.

This only works if I resize very slowly, otherwise it all starts to mess up.

Correct Image http://imageshack.us/a/img585/105/50186779.png
Correct Image http://imageshack.us/a/img267/3179/76648216.png

I must be obviously doing something wrong, the code below is the canvas:

using System;
using System.Windows;
using System.Windows.Controls;

namespace WpfApplication1.View
{
    public class TileCanvas : Canvas
    {
        #region Fields

        private Boolean _firstCreation;
        private Double _heightDifference;
        private Double _widthDifference;

        #endregion // Fields

        #region Properties

        public static readonly DependencyProperty TileSizeProperty =
            DependencyProperty.Register("TileSize", typeof (Int32), typeof (TileCanvas),
                                        new PropertyMetadata(30));

        public Int32 TileSize
        {
            get { return (Int32) GetValue(TileSizeProperty); }
            set { SetValue(TileSizeProperty, value); }
        }

        public Int32 Columns { get; private set; }
        public Int32 Rows { get; private set; }

        #endregion // Properties

        #region Constructors

        public TileCanvas()
        {
            _firstCreation = true;
        }

        #endregion // Constructors

        #region Methods

        #region Public

        #endregion // Methods - Public

        #region Protected

        /// <summary>
        /// Gets called when the rendering size of this control changes.
        /// </summary>
        /// <param name="sizeInfo">Information on the size change.</param>
        protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
        {
            Console.WriteLine("{0}x{1}", sizeInfo.NewSize.Width, sizeInfo.NewSize.Height);

            _widthDifference += sizeInfo.NewSize.Width - sizeInfo.PreviousSize.Width;
            _heightDifference += sizeInfo.NewSize.Height - sizeInfo.PreviousSize.Height;

            int columnDifference = 0;
            int rowDifference = 0;

            if (_widthDifference > TileSize || _widthDifference < TileSize*-1)
            {
                columnDifference = ((Int32) _widthDifference)/TileSize;
                Columns += columnDifference;
                _widthDifference = 0;
            }

            if (_heightDifference > TileSize || _heightDifference < TileSize*-1)
            {
                rowDifference = ((Int32) _heightDifference)/TileSize;
                Rows += rowDifference;
                _heightDifference = 0;
            }

            UpdateTileSet(columnDifference, rowDifference);
        }

        #endregion // Methods - Protected

        #region Private

        /// <summary>
        /// Updates the number of tiles if the control changed in size.
        /// </summary>
        private void UpdateTileSet(Int32 columnChange, Int32 rowChange)
        {
            // Exit if there's no practical change
            if (columnChange == 0 && rowChange == 0)
            {
                return;
            }

            // Delete all tiles that fall out on vertical resize
            if (rowChange < 0)
            {
                for (var row = Rows; row > Rows + rowChange; row--)
                {
                    for (var column = 0; column < Columns; column++)
                    {
                        Children.RemoveRange(GetListIndex(0, Rows), Columns);
                    }
                }
            }

            // Delete all tiles that fall out on horizontal resize
            if (columnChange < 0)
            {
                for (var column = Columns; column > Columns + columnChange; column--)
                {
                    for (var row = 0; row < Rows; row++)
                    {
                        Children.RemoveAt(GetListIndex(column, row));
                    }
                }
            }

            // Fill new rows with tiles on vertical resize
            if (rowChange > 0)
            {
                for (var row = Rows - rowChange; row < Rows; row++)
                {
                    for (var column = 0; column < Columns; column++)
                    {
                        var tile = new Tile(column, row);

                        Point position = GetCanvasPosition(column, row);
                        var index = GetListIndex(column, row);

                        SetLeft(tile, position.X);
                        SetTop(tile, position.Y);

                        Children.Insert(index, tile);
                    }
                }
            }

            // The first population is a special case that can be handled
            // by filling rows only.
            if (_firstCreation)
            {
                _firstCreation = false;
                return;
            }

            // Fill new columns with tiles on horizontal resize
            if (columnChange > 0)
            {
                for (var column = Columns - columnChange; column < Columns; column++)
                {
                    for (var row = 0; row < Rows; row++)
                    {
                        var tile = new Tile(column, row);

                        Point position = GetCanvasPosition(column, row);
                        var index = GetListIndex(column, row);

                        SetLeft(tile, position.X);
                        SetTop(tile, position.Y);

                        Children.Insert(index, tile);
                    }
                }
            }
        }

        /// <summary>
        /// Returns the index a tile should occupy based on its column and row.
        /// </summary>
        /// <param name="column">The column in which this tile resides.</param>
        /// <param name="row">The row in which this tile resides.</param>
        /// <returns></returns>
        private Int32 GetListIndex(Int32 column, Int32 row)
        {
            return row*Columns + column;
        }

        /// <summary>
        /// Returns the coordinates of a specific position.
        /// </summary>
        /// <param name="column">The column the position is in.</param>
        /// <param name="row">The row the position is in.</param>
        /// <returns></returns>
        private Point GetCanvasPosition(Int32 column, Int32 row)
        {
            var positionX = column*TileSize;
            var positionY = row*TileSize;

            return new Point(positionX, positionY);
        }

        #endregion // Methods - Private

        #endregion // Methods
    }
}

This is Tile.cs

using System;
using System.Windows.Controls;

namespace WpfApplication1.View
{
    /// <summary>
    /// Interaction logic for Tile.xaml
    /// </summary>
    public partial class Tile : Border
    {
        public Tile(Int32 column, Int32 row)
        {
            InitializeComponent();


            Textfield.Text = String.Format("{0}x{1}", column, row);
        }
    }
}

and Tile.xaml

<Border x:Class="WpfApplication1.View.Tile"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             Width="40" Height="40">
    <TextBlock x:Name="Textfield" FontSize="8" HorizontalAlignment="Center" VerticalAlignment="Top"/>
</Border>

Where’s the issue?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T21:13:15+00:00Added an answer on June 16, 2026 at 9:13 pm

    Here’s a good source of information about how to place elements at custom positions in a WPF control: http://codeblitz.wordpress.com/2009/03/20/wpf-auto-arrange-animated-panel/

    I don’t know if it applies to canvas, but placing your code inside the Measure/Arrange Override methods might yield better results.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom View that uses Paint and Canvas to draw objects. My
I have a custom view that fills my entire screen. (A piano keyboard) When
Problem: I have a custom Calendar view that I generated using canvas drawing and
I have some code that does custom drawing. Basically it is form fill program
I have defined a custom canvas style component, using JPanel, that will support the
We have a custom canvas which has specialized nodes that behave a lot like
So I have an custom overlay item that I have written to fill in
I have a drawing activity that creates a custom 'canvas' View that occupies the
I have created a custom product configurator that saves a canvas element as a
I have a custom canvas that I can drop controls on, move, and resize.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.