Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>That's because <code>UIElementCollection</code> (the type of the <code>Children</code> property) doesn't raise notifications when a new item is added or removed, so the binding isn't refreshed</p> <p>You could, however, create your own custom <code>UniformGrid</code>, and override the <code>CreateUIElementCollection</code> property to create an instance of a custom collection that inherits <code>UIElementCollection</code> and implements <code>INotifyCollectionChanged</code>.</p> <p>Here's a basic implementation :</p> <p><strong>ObservableUIElementCollection</strong></p> <pre><code>public class ObservableUIElementCollection : UIElementCollection, INotifyCollectionChanged, INotifyPropertyChanged { public ObservableUIElementCollection(UIElement visualParent, FrameworkElement logicalParent) : base(visualParent, logicalParent) { } public override int Add(UIElement element) { int index = base.Add(element); var args = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, element, index); OnCollectionChanged(args); OnPropertyChanged("Count"); OnPropertyChanged("Item[]"); return index; } public override void Clear() { base.Clear(); OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); OnPropertyChanged("Count"); OnPropertyChanged("Item[]"); } public override void Insert(int index, UIElement element) { base.Insert(index, element); var args = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, element, index); OnCollectionChanged(args); OnPropertyChanged("Count"); OnPropertyChanged("Item[]"); } public override void Remove(UIElement element) { int index = IndexOf(element); if (index &gt;= 0) { RemoveAt(index); var args = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, element, index); OnCollectionChanged(args); OnPropertyChanged("Count"); OnPropertyChanged("Item[]"); } } public override UIElement this[int index] { get { return base[index]; } set { base[index] = value; var args = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, value, index); OnCollectionChanged(args); OnPropertyChanged("Item[]"); } } public event NotifyCollectionChangedEventHandler CollectionChanged; public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e) { var handler = CollectionChanged; if (handler != null) handler(this, e); } protected virtual void OnPropertyChanged(string propertyName) { var handler = PropertyChanged; if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); } } </code></pre> <p><strong>MyUniformGrid</strong></p> <pre><code>public class MyUniformGrid : UniformGrid { protected override UIElementCollection CreateUIElementCollection(FrameworkElement logicalParent) { return new ObservableUIElementCollection(this, logicalParent); } } </code></pre> <p><strong>XAML</strong></p> <pre><code>&lt;local:MyUniformGrid x:Name="MyUniformGrid" Rows="{Binding RelativeSource={RelativeSource Self}, Path=Children.Count, Converter={StaticResource CountToDimensionConverter}, ConverterParameter=R}" Columns="{Binding RelativeSource={RelativeSource Self}, Path=Children.Count, Converter={StaticResource CountToDimensionConverter}, ConverterParameter=C}"&gt; &lt;Button Content="Hello, World!" /&gt; &lt;Button Content="Hello, World!" /&gt; &lt;Button Content="Hello, World!" /&gt; &lt;Button Content="Hello, World!" /&gt; &lt;Button Content="Hello, World!" /&gt; &lt;Button Content="Hello, World!" /&gt; &lt;/local:MyUniformGrid&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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