I have a grid that I dynamically need to resize in the number of columns based on the size of my window.
The only way I have found to make it work is using:
FrameworkElementFactory fGrid = new FrameworkElementFactory
(typeof(System.Windows.Controls.Primitives.UniformGrid));
fGrid.SetValue(System.Windows.Controls.Primitives.UniformGrid.ColumnsProperty, columns);
listbox.ItemsPanel = new ItemsPanelTemplate(fGrid);
In my listbox_SizeChanged event handler. Sadly it gives out the following (harmless) errors. Any better ideas for how to change it dynamically?
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''. BindingExpression:Path=HorizontalContentAlignment; DataItem=null; target element is 'ListViewItem' (Name=''); target property is 'HorizontalContentAlignment' (type 'HorizontalAlignment')