Synchronizing ColumnDefinitions

njsokalski

Well-known member
Joined
Mar 16, 2011
Messages
102
Programming Experience
5-10
I have XAML similar to the following:

<Grid Name="GridOne" HorizontalAlignment="Center">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<ScrollViewer Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="{Binding ElementName=GridOne,Path=ColumnDefinitions.Count}">
<Grid Name="GridTwo">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
</Grid>
</ScrollViewer>
</Grid>

The ColumnDefinitions for GridOne are dynamically generated, and I want the ColumnDefinitions for GridTwo to be the same. I thought about doing this through with a binding, but I wasn't exactly sure how. Right now I just use the following code in my codebehind's Loaded event:

For i As Integer = 0 To Me.GridOne.ColumnDefinitions.Count - 1
Me.GridTwo.ColumnDefinitions(i).Width = New GridLength(Me.GridOne.ColumnDefinitions(i).ActualWidth, GridUnitType.Pixel)
Next

However, this has been giving me strange results. Using a few System.Diagnostics.Debug.WriteLine() statements, I have determined that the assignment in the For loop in the Loaded event is being successfully performed. However, the columns in GridOne and GridTwo are not being displayed as the same size. What am I missing here? Any help would be appreciated. Thanks.
 
Top Bottom