Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>For this purpose I generally use an expression binding class I wrote called "edf:Visibility" that allows me to do this:</p> <pre><code>&lt;ItemsControl ItemsSource="{Binding Polygons}" AlternationCount="1000000"&gt; &lt;ItemsPanel&gt; &lt;ItemsPanelTemplate&gt; &lt;Canvas /&gt; &lt;/ItemsPanelTemplate&gt; &lt;/ItemsPanel&gt; &lt;ItemContainerStyle&gt; &lt;Style&gt; &lt;Setter Property="Visibility" Value="{edf:Visibility self.AlternationIndex &amp;lt; context.N}" /&gt; &lt;/Style&gt; &lt;/ItemContainerStyle&gt; &lt;/ItemsControl&gt; </code></pre> <p>The same thing can be done using a standard binding and a converter as follows:</p> <pre><code>... &lt;Setter Property="Visibility" Value="{Binding RelativeSource={RelativeSource Self}, Converter={x:Static AlternationIndexComparer.Instance}}" /&gt; ... </code></pre> <p>Of course in this case you have to write the converter yourself.</p> <p>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:</p> <pre><code>&lt;PathGeometry&gt; &lt;PathGeometry.Transform&gt; &lt;ScaleTransform ScaleX="{Binding N}" /&gt; &lt;/PathGeometry.Transform&gt; ... &lt;/PathGeometry&gt; </code></pre> <p>From your description of the problem this doesn't appear to apply in your case.</p> <p>A general solution is to create an attached property with this functionality which can simply be used like this:</p> <pre><code>&lt;Canvas local:MyAttachedProperties.ChildrenVisible="{Binding N}"&gt; ... &lt;/Canvas&gt; </code></pre> <p>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:</p> <pre><code>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) =&gt; { UpdateVisibility((Panel)obj); if(((int)e.OldValue)==int.MaxValue) ((UIElement)obj).LayoutUpdated += (obj2, e2) =&gt; UpdateVisibility((Panel)obj2); } }); static void UpdateVisibility(Panel panel) { int n = GetChildrenVisible(panel); int i = 0; foreach(var child in panel.Children) child.Visibility = (i++ &lt; n) ? Visibility.Visible : Visibility.Collapsed; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload