Reputation: 182
I have a Grid
that has some column defined and Column width is also defined in xaml
.
<Border Grid.Row="2" BorderThickness="2" BorderBrush="#001732"
Width="{Binding ActualWidth, ElementName=ParentContainer}">
<Grid Name="DataGrid" Width="{Binding ActualWidth, ElementName=ParentContainer}"
Style="{StaticResource GridStyle}">
<ColumnDefinition Width="50*" />
<ColumnDefinition Width="50*" />
<ColumnDefinition Width="50*" />
<ColumnDefinition Width="50*" />
<ColumnDefinition Width="50*" />
<ColumnDefinition Width="50*" />
<ColumnDefinition Width="50*" />
</Grid.ColumnDefinitions>
</Grid>
</Border>
now if i change the size of window the column width changes automatically. and i am trying to get the changed latest width with this code.
public void CoulmnWidthSetter()
{
_columnWidth = DataGrid.ColumnDefinitions[4].Width.Value;
foreach (var col in DataGrid.ColumnDefinitions)
{
_totalColumnsWidth = _totalColumnsWidth + col.Width.Value;
}
}
and calling the Column width setter method in below Event.
private void ParentContainer_OnSizeChanged(object sender, SizeChangedEventArgs e)
{
CoulmnWidthSetter();
}
or i have tried to call this method in Window size changed event also. But still it always give me that hard coded 50
width value why ?
Upvotes: 0
Views: 2534
Reputation: 6749
In WPF Width
and ActualWidth
are two different things.
What you're looking for here is the ActualWidth
of your ColumnDefinition
s.
The ActualWidth
is a double
value that represents that length at that moment in time.
Signature:
public double ActualWidth { get; }
The Width
is a GridLength
type; that can be set by various means; including hard coded values, Star values, or Auto. You set the Width
(a GridLength
type) to give the ColumnDefinition
the information needed to dynamically adjust itself accordingly or to remain constant. It is not intended to provide you with the current value but instead give the owner an expressive way of setting it's values.
Examples:
columnDefinition.Width = new GridLength(50); //Hard coded to 50
columnDefinition.Width = new GridLength(50, GridUnitType.Star); //dividend of remaining space allocated at 50.
//In other words; any other Star values will be divided in Width based on the remaining Grid size and the Star value given.
//A star value of 25* in another ColumnDefinition will make it half the length of the 50* and a value of 100* will make it twice the 50* or the 50* half of the 100*.
//Either way you look at it; the total remaining Width of the space provided by the panel gets divided out to the Star Columns and the dividend they get is based on their portion of the Star. 50* for all Columns will make them all equal.
columnDefinition.Width = GridLength.Auto; //Adjusts to the needed width dynamically. As your content grows so does the Width.
Width
is also a DependencyProperty
so it allows Binding
and must be set on the UI thread.
Also; just a suggestion: You shouldn't need to set the size that way just because your view is dynamically changing size. This is what the * star is for in the value setting. There are also other means of doing this built in but without knowing exactly why you're doing it; have at it.
Upvotes: 2