I have a grid whose width is set to 250. The Width of the Window is 600. The Left and Right margins of the grid are set to 50 and 400. So the rendered grid width is reduced to 600-(50+400) = 150.
I want to know how can I find the correct visible width of the first grid which is 150. However, none of the following properties is returning 150.
g1.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
Debug.WriteLine($"g1.DesiredSize.Width={g1.DesiredSize.Width}"); // Returns 700
Debug.WriteLine($"g1.ActualWidth={g1.ActualWidth}"); //Returns 250
Debug.WriteLine($"g1.RenderSize.Width={g1.RenderSize.Width}"); //Returns 250
Debug.WriteLine($"g1.Width={g1.Width}"); //Returns 250
Xaml Code is
<Window Title="MainWindow" Width="600" Height="300">
<StackPanel>
<Grid Name ="g1" Margin="50,0,400,0" Background="GreenYellow" Width="250" Height="50"/>
<Grid Name ="g2" Margin="50,0,400,0" Background="LightBlue" Width="150" Height="50"/>
</StackPanel>
</Window>
Output is:

Ok, I think the issue is because of
ClipToBoundsissue. So actually the grid is rendered to the same size of 250 as defined, however, only portion of 150 width is visible and the remaining grid is clipped. More explanation can be found here on stackoverflowIf you don't want the Grid to be clipped so that the whole grid of 250 widths is visible then there are three choices
Replace Grid with
Canvascontrol which is not affected byClipToBoundsissue.Create a custom grid that overrides the
GetLayoutClip()to return null when ClipToBounds=falsePlace the grid inside the Canvas control
Things become more clear if you place a border inside your grid, then you can see that 1'st grid is infact clipped instead of rendered to width of 150.
Note: If you are overriding the
GetLayoutClip()then make sure that you override it on the control that is getting clipped and not on the container. Like in the First example it was Grid inside StackPanel that was getting trimmed so we created CustomGridMyGrid, while in the last example, it is the button that is getting clipped so we created custom buttonMyButtonthat overridesGetLayoutClip()Here is the Output:
Here is the MainWindow.xaml
Here is the code for CustomGrid MyGrid.cs
Here is the code for CustomButton MyButton.cs