If you run the sample window below the top ItemsControl, it will update the layout in a few seconds, until finally all the columns have the correct width (correctly = identical to the columns inside the bottom ItemsControl).
You can change the width of the window and scroll the bottom ItemsControls around the ScrollViewer both horizontally and vertically - but as soon as you change the height of the window, the layout will flip for a few seconds.
Note. There is no dimensional uncertainty, as in other matters where the grid infinitely updates the dimensions.
I'm doing something wrong - and if so, how can I fix it? - or should I send this problem to Microsoft-Connect?
Code behind:
namespace DynamicGridColumnBinding { using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Windows; using System.Windows.Controls; public partial class MainWindow { private static readonly CultureInfo[] cultureInfos = CultureInfo.GetCultures(CultureTypes.NeutralCultures).Take(15).ToArray(); public MainWindow() { this.InitializeComponent(); } public static IEnumerable<CultureInfo> AllCultures { get { return cultureInfos; } } private void GridInitialized(object sender, EventArgs e) { var grid = (Grid)sender; for ( int i = 0; i < cultureInfos.Length; i++ ) grid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto, SharedSizeGroup = "g" + i, }); } private void ScrollViewerScrollChanged(object sender, ScrollChangedEventArgs e) { if ( e.HorizontalChange != 0 ) this.legendScroller.ScrollToHorizontalOffset(e.HorizontalOffset); } } }
Xaml:
<FrameworkElement.Resources> <ItemsPanelTemplate x:Key="panelTemplate"> <Grid Initialized="GridInitialized" /> </ItemsPanelTemplate> <Style TargetType="ContentPresenter" x:Key="containerStyle"> <Setter Property="Tag" Value="{Binding RelativeSource={RelativeSource Self}, Path=(ItemsControl.AlternationIndex)}" /> <Setter Property="Grid.Column" Value="{Binding RelativeSource={RelativeSource Self}, Path=(ItemsControl.AlternationIndex)}" /> </Style> <Style TargetType="TextBlock" x:Key="textStyle"> <Setter Property="Padding" Value="5" /> <Setter Property="Background" Value="Lime" /> <Setter Property="HorizontalAlignment" Value="Center" /> <Setter Property="VerticalAlignment" Value="Center" /> </Style> </FrameworkElement.Resources> <DockPanel Grid.IsSharedSizeScope="True" DataContext="{Binding Source={x:Static local:MainWindow.AllCultures}}"> <ScrollViewer DockPanel.Dock="Top" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Disabled" x:Name="legendScroller"> <ItemsControl ItemsSource="{Binding}" AlternationCount="{x:Static System:Int32.MaxValue}" Margin="0 0 500 0" ItemsPanel="{StaticResource panelTemplate}" ItemContainerStyle="{StaticResource containerStyle}"> <ItemsControl.ItemTemplate> <DataTemplate DataType="{x:Type glob:CultureInfo}"> <GroupBox Header="{Binding Name}" HeaderStringFormat="[ {0} ]"> <TextBlock Style="{StaticResource textStyle}" Text="{Binding Tag, RelativeSource={RelativeSource AncestorType=ContentPresenter, AncestorLevel=2}}" /> </GroupBox> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> </ScrollViewer> <TextBlock Foreground="Red" DockPanel.Dock="Top" Margin="0 10" FontSize="20" Text="some random arbitrary content in between" /> <ScrollViewer HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Auto" ScrollChanged="ScrollViewerScrollChanged"> <ItemsControl ItemsSource="{Binding}" AlternationCount="{x:Static System:Int32.MaxValue}" ItemsPanel="{StaticResource panelTemplate}" ItemContainerStyle="{StaticResource containerStyle}"> <ItemsControl.ItemTemplate> <DataTemplate DataType="{x:Type glob:CultureInfo}"> <Border Background="DodgerBlue" Padding="5" Margin="1"> <GroupBox Header="{Binding DisplayName}"> <TextBlock Style="{StaticResource textStyle}" Padding="5 100" Text="{Binding Tag, RelativeSource={RelativeSource AncestorType=ContentPresenter, AncestorLevel=2}}" /> </GroupBox> </Border> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> </ScrollViewer> </DockPanel>
BTW: If you force the elements of the upper ItemsControl to set size (by adding MinWidth="200" to the GroupBox), then the lower ElementsControl will act stupidly.
BTW2: Starting from approx. 8 columns of total size (in sample 15 controlled by .Take(15) ), you see that the rearrangement appears and it doubles in time with each added column, so 20 columns almost do not end for minutes.
BTW3: Getting more than one comment for 3 months is very frustrating.