- realesmedia
- Posts: 85
- Joined:
Get Grid.ColumnDefinition width
Hi
How to find the width of a Grid.ColumnDefinition in pixels if in xaml <ColumnDefinition Width = "Auto"/> or <ColumnDefinition Width = "*"/>
all methods ColumnDefinition return 0.0f
ColumnDefinition :: GetActualWidth ()
ColumnDefinition :: GetWidth ()
ColumnDefinition :: GetRenderSize ()
How to find the width of a Grid.ColumnDefinition in pixels if in xaml <ColumnDefinition Width = "Auto"/> or <ColumnDefinition Width = "*"/>
all methods ColumnDefinition return 0.0f
ColumnDefinition :: GetActualWidth ()
ColumnDefinition :: GetWidth ()
ColumnDefinition :: GetRenderSize ()
-
sfernandez
Site Admin
- Posts: 2984
- Joined:
Re: Get Grid.ColumnDefinition width
To get the actual width (or height) of a Grid's column (row) you have to place an element in that cell and ask for its ActualWidth (ActualHeight). Columns (Rows) are not measured, only the elements inside the Grid, using the information from Column and Row definitions.
Code: Select all
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Decorator x:Name="FirstColumnRef" Grid.Column="0"/>
...
</Grid>
Code: Select all
Loaded += (s, e) =>
{
FrameworkElement firstColumnRef = (FrameworkElement)FindName("FirstColumnRef");
Console.WriteLine("FirstColumnRef.ActualWidth = " + firstColumnRef.ActualWidth);
};
- realesmedia
- Posts: 85
- Joined:
Re: Get Grid.ColumnDefinition width
In this case:
Code: Select all
<Grid Background="Red">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.5*"/>
<ColumnDefinition Width="6"/>
<ColumnDefinition Width="0.5*"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="1" Background="Black"/>
</Grid>
auto w = Grid::GetColumnDefinitions()->Get(0)->GetActualWidth(); // 0.f ???
-
sfernandez
Site Admin
- Posts: 2984
- Joined:
Re: Get Grid.ColumnDefinition width
If you have this Grid and want to know the size of the first column, then you have to place an (invisible) element in that column:
And in code you would look for that element after layout has finished:
Code: Select all
<Grid Background="Red">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.5*"/>
<ColumnDefinition Width="6"/>
<ColumnDefinition Width="0.5*"/>
</Grid.ColumnDefinitions>
<Decorator x:Name="FirstColumnRef" Grid.Column="0"/>
<Grid Grid.Column="1" Background="Black"/>
</Grid>
Code: Select all
Loaded() += [](BaseComponent* sender, const RoutedEventArgs& e)
{
FrameworkElement* firstColumnRef = FindName<FrameworkElement>("FirstColumnRef");
m_firstColumnWidth = firstColumnRef->GetActualWidth();
};
Re: Get Grid.ColumnDefinition width
Marking as solved!
Who is online
Users browsing this forum: No registered users and 78 guests