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 1038795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:01:12+00:00 2026-05-16T15:01:12+00:00

In GDI+/WinForms, I could do this in the Click() event and using the graphics

  • 0

In GDI+/WinForms, I could do this in the Click() event and using the graphics Object:

AddPoint(p); //Add Point contains some code to make sure there is only 3 dots
foreach (Point p in PointList) {
    DrawRectangle(p);
}
Invalidate();

If I try something similar in WPF, it won’t cleanup the dots I created (I’m guessing because of how WPF works). What this means is if I check to make sure there is only three dots at a time, and pop off the oldest point to make room for the new one, the rectangle drawn would be still there.

So the question is, how can I create something in WPF that allows me to

  • Draw a rectangle at a Point
  • Remove rectangles/points from a WPF canvas after there is more than 3
  • 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-05-16T15:01:12+00:00Added an answer on May 16, 2026 at 3:01 pm

    You’re doing WPF the WinForms way. Don’t do that. It’s like writing VB code in C++. It can only end in tears.

    To do this the WPF way, use databinding and a view model class to do the logic of “no more than 3 at a time.” Then, for the UI, just bind to the PointList in your view model.

    Here’s what my XAML should look like. Notice I’m just using an ItemsControl and a Canvas, then binding the ItemsSource to PointList:

    <Window x:Class="WpfTester.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
    
        <ItemsControl ItemsSource="{Binding Path=PointList}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <Canvas />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Rectangle Fill="Red" Width="25" Height="25" />
                </DataTemplate>
            </ItemsControl.ItemTemplate>
            <ItemsControl.ItemContainerStyle>
                <Style>
                    <Setter Property="Canvas.Left" Value="{Binding Path=X}" />
                    <Setter Property="Canvas.Top" Value="{Binding Path=Y}" />
                </Style>
            </ItemsControl.ItemContainerStyle>
        </ItemsControl>
    </Window>
    

    Then we just need to create the PointList. We’ll use the normal WPF means: a view model class to hold the point list:

    class MainViewModel
    {
        public MainViewModel()
        {
            PointList = new ObservableCollection<Point>();
    
            // Some example data:
            AddPoint(new Point(10, 10));
            AddPoint(new Point(200, 200));
            AddPoint(new Point(500, 500));
        }
    
        public ObservableCollection<Point> PointList { get; private set; }
    
        public void AddPoint(Point p)
        {
            // 3 at most, please!
            if (PointList.Count == 3)
            {
                PointList.RemoveAt(0);
            }
            PointList.Add(p);
        }
    }
    

    Piece of cheese, yeah? So the final part is just telling the XAML to load your view model. Inside the the code-behind for your XAML, set the DataContext to your view model:

    // Inside MainWindow.xaml.cs
    public MainWindow()
    {
        InitializeComponent();
    
        // Add this line:
        this.DataContext = new MainViewModel();
    }
    

    Now that you’ve got that in place, you can add/remove rectangles anywhere in your code simply by calling viewModel.AddPoint or viewModel.PointList.Remove, and the UI will automatically update to reflect the changes.

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

Sidebar

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.