Image size when changing parents
I have a border control as a parent to the grid and when I set the rowspan and colspan it sizes correctly. Then I change it's parent to a canvas and now the size is very very very small. I can barely see it, but it's there. How can I keep the width/height to be the same size as it was when the grid was it's parent?
-
-
sfernandez
Site Admin
- Posts: 3222
- Joined:
Re: Image size when changing parents
I recommend you start reading this tutorial about Layout and Panels.
Basically each container determines how children are measured and arranged, so the same element can look different depending on which container you place it in.
Grid lets the children to fill the entire cell where they are placed. Canvas instead, asks the children to use the minimum required size.
But you can specify the size of an element explicitly by setting the Width and Height properties of an element:
Basically each container determines how children are measured and arranged, so the same element can look different depending on which container you place it in.
Grid lets the children to fill the entire cell where they are placed. Canvas instead, asks the children to use the minimum required size.
But you can specify the size of an element explicitly by setting the Width and Height properties of an element:
Code: Select all
Ptr<Border> border = *new Border();
border->SetWidth(gridBorder->GetActualWidth());
border->SetHeight(gridBorder->GetActualHeight());
Re: Image size when changing parents
So when I try the ActualWidth/Height in C# with VS it works when the window starts and isn't maximized. When I max the window and everything scales correctly (even the rect's that are inside the inventory grid) the icon size is still the original starting size. Should ActualWidth adjust when the window gets maximized and scales up? Haven't had a chance to test this with Noesis so instead I'm testing with VS and normal WPF as I have access to that currently.
-
-
sfernandez
Site Admin
- Posts: 3222
- Joined:
Re: Image size when changing parents
ActualWidth/Height properties are recalculated everytime an element is measured and arranged. This occurs when a property that affects layout (Width, Height, Margin, Horizontal/VerticalAlignment, ...) are changed in this element, or when the element's container changes its size, so children need to be repositioned.
But ActualWidth/Height are not affected by transforms, for example, when elements are inside a Viewbox:
In the previous example, the "grid" will always measure the same size, as its container (Border) has a fixed Width (100) and Height (200).
If this is your case, you have to apply the same scale transform that is applied to the "grid" to the icon:
But ActualWidth/Height are not affected by transforms, for example, when elements are inside a Viewbox:
Code: Select all
<Grid
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Viewbox Margin="100" HorizontalAlignment="Left">
<Border Width="100" Height="200">
<Grid x:Name="grid" Background="Silver"/>
</Border>
</Viewbox>
<StackPanel HorizontalAlignment="Left" Margin="10">
<TextBlock Text="{Binding ActualWidth, ElementName=grid}" FontSize="30"/>
<TextBlock Text="{Binding ActualHeight, ElementName=grid}" FontSize="30"/>
</StackPanel>
</Grid>
If this is your case, you have to apply the same scale transform that is applied to the "grid" to the icon:
Code: Select all
Noesis::Math::Matrix4f transformToGrid = rootCanvas->TransformToDescendant(grid);
Ptr<Border> border = *new Border();
border->SetWidth(gridBorder->GetActualWidth() * transformToGrid[0][0]);
border->SetHeight(gridBorder->GetActualHeight() * transformToGrid[1][1]);
Re: Image size when changing parents
Do you happen know the C# equivalent to that? I practice things in C#/VS and it looks like TransformToDescendant() in C# returns a GeneralTransform. I'm not a big math guy but I do seem to recall Matrix holding position, rotation, scale right? So I assume what you are doing is taking the scale part of the matrix and applying that?
-
-
sfernandez
Site Admin
- Posts: 3222
- Joined:
Re: Image size when changing parents
Yes, I was just using the scale values from the matrix to calculate the appropriate size for the border that will be placed directly in the root canvas.
Who is online
Users browsing this forum: Google [Bot], Semrush [Bot] and 2 guests