#432 – Height and Width vs. ActualHeight and ActualWidth
November 17, 2011 2 Comments
Specifying the height and width of a control using the Height and Width properties indicates the desired height and width. The layout system will take the requested height and width into account, but the actual height and width depends on a number of factors.
You can use the ActualHeight and ActualWidth properties to get at the actual height and width, after layout.
<StackPanel> <Button Name="btnA" HorizontalAlignment="Stretch" Content="Get Info" Click="btnA_Click"/> <Button Name="btnB" HorizontalAlignment="Center" Width="1in" Content="Push or Pull Me"/> <Button Name="btnC" Width="175" MaxWidth="{Binding ElementName=btnB, Path=ActualWidth}" Content="Push Me"/> </StackPanel>
Clicking on GetInfo, we display info about the third button. ActualWidth is smaller than the requested Width, because it can’t be wider than the second button.
The first button’s Width is set to “NaN” (Not a Number), indicating that Width is set to Auto.
The second button’s width was set to 1 inch, which is 96 units on my 96 dpi display.