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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:39:08+00:00 2026-05-14T07:39:08+00:00

There is a canvas element with 10 children (polygons). There is a number in

  • 0

There is a canvas element with 10 children (polygons). There is a number in my code from 1 to 10.

I’d like to draw the first N elements from the children elements, base on my number.

It would be the best solution to do this from XAML with a minimum code-behind like this:

...
MyCanvas.N = 5;
...
  • 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-14T07:39:08+00:00Added an answer on May 14, 2026 at 7:39 am

    For this purpose I generally use an expression binding class I wrote called “edf:Visibility” that allows me to do this:

    <ItemsControl ItemsSource="{Binding Polygons}" AlternationCount="1000000">
      <ItemsPanel>
        <ItemsPanelTemplate>
          <Canvas />
        </ItemsPanelTemplate>
      </ItemsPanel>
      <ItemContainerStyle>
        <Style>
          <Setter Property="Visibility"
                  Value="{edf:Visibility self.AlternationIndex &lt; context.N}" />
        </Style>
      </ItemContainerStyle>
    </ItemsControl>
    

    The same thing can be done using a standard binding and a converter as follows:

    ...
          <Setter Property="Visibility"
                  Value="{Binding RelativeSource={RelativeSource Self},
                           Converter={x:Static AlternationIndexComparer.Instance}}" />
    ...
    

    Of course in this case you have to write the converter yourself.

    If your polygons are all fixed width or height and evenly spaced, an easy way to implement this with no code is to use a clipping geometry and transform its width (or height) by a factor of N to cause it to show only N polygons. The XAML for the clipping geometry has a transform like this:

    <PathGeometry>
      <PathGeometry.Transform>
        <ScaleTransform ScaleX="{Binding N}" />
      </PathGeometry.Transform>
      ...
    </PathGeometry>
    

    From your description of the problem this doesn’t appear to apply in your case.

    A general solution is to create an attached property with this functionality which can simply be used like this:

    <Canvas local:MyAttachedProperties.ChildrenVisible="{Binding N}">
      ...
    </Canvas>
    

    in this case you have to create the ChildrenVisible property, but you only have to code it once and it will work for any panel (not just canvas). Here is the technique in detail:

    public class MyAttachedProperties
    {
      ... GetChildrenVisible ...  // use propa snippet to implement attached property
      ... SetChildrenVisible ...
      ... RegisterAttached("ChildrenVisible", typeof(int), typeof(MyAttachedProperties), new PropertyMetadata
      {
        DefaultValue = int.MaxValue,
        PropertyChangedCallback = (obj, e) =>
        {
          UpdateVisibility((Panel)obj);
          if(((int)e.OldValue)==int.MaxValue)
            ((UIElement)obj).LayoutUpdated += (obj2, e2) => UpdateVisibility((Panel)obj2);
        }
      });
    
      static void UpdateVisibility(Panel panel)
      {
        int n = GetChildrenVisible(panel);
        int i = 0;
        foreach(var child in panel.Children)
          child.Visibility = (i++ < n) ? Visibility.Visible : Visibility.Collapsed;
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know how to align a canvas element to the absolute
I am currently developing a little sketching application based on HTML5 Canvas element. There
Context: There's an application where you draw things on canvas. Where user clicks there's
I'm trying to draw an animation over google maps, hopefully using html5's canvas element.
I have the script that works with pixels in html5 canvas element. And there
I'm having one thread that generates GUI elements in wpf. A canvas is there
Is there a way to export a canvas element with multiple objects to html
I was wondering what would be the best way to execute a java-script code
Is there a Javascript-y way to provide a downloadable link for a Canvas element?
At first I was really excited about HTML5's canvas element. But It will not

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.