I’m using a binding of the form
{Binding RelativeSource={RelativeSource Self}, Path=Children.Count, Converter={StaticResource CountToDimensionConverter}, ConverterParameter=Rows}
Despite adding the children in xaml, when I break in the converter, the value is always 0.
What I assume is going on is that the children are not added until after this binding is called.
I also assume that the binding is broken after it’s been called once because .Count is a readonly property (I’ve had a similar problem before where I had to add an empty setter in the property to maintain the binding and fool WPF) hence the binding not updating once children are added.
However, I’m stuck on the bit where you come up with a solution for the problem and make it work… =/
<UniformGrid 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}">
<Button Content="Hello, World!" />
<Button Content="Hello, World!" />
<Button Content="Hello, World!" />
<Button Content="Hello, World!" />
<Button Content="Hello, World!" />
<Button Content="Hello, World!" />
</UniformGrid>
Thanks,
Rabit
That’s because
UIElementCollection(the type of theChildrenproperty) doesn’t raise notifications when a new item is added or removed, so the binding isn’t refreshedYou could, however, create your own custom
UniformGrid, and override theCreateUIElementCollectionproperty to create an instance of a custom collection that inheritsUIElementCollectionand implementsINotifyCollectionChanged.Here’s a basic implementation :
ObservableUIElementCollection
MyUniformGrid
XAML