Reputation: 119
How I can determine actual width and actual height of control, after applying the transformation?
For example, I think that following XAML-code should show size something like 400x400 (for example, on the pic. red rectangle, which width 400), but width and height equals 200.
What am I doing wrong?
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
x:Class="VisualCad.Components.TempVisualTests.MainWindow"
mc:Ignorable="d"
x:Name="MyWin"
Title="MainWindow" WindowState="Maximized" Height="500" Width="500">
<Grid>
<Rectangle Width="400" Height="5" Fill="Red" Margin="0,20,0,0" VerticalAlignment="Top"/>
<Canvas x:Name="MyCanvas" Width="200" Height="200">
<Canvas.RenderTransform>
<ScaleTransform CenterX="100" CenterY="100" ScaleX="2" ScaleY="2" />
</Canvas.RenderTransform>
<StackPanel>
<TextBlock Text="{Binding Path=RenderSize, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Canvas}}, StringFormat='Render size: {0}'}" />
<TextBlock Text="{Binding Path=ActualWidth, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Canvas}}, StringFormat='ActualWidth: {0}'}" />
<TextBlock Text="{Binding Path=ActualHeight, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Canvas}}, StringFormat='ActualHeight: {0}'}" />
</StackPanel>
</Canvas>
</Grid>
</Window>
Upvotes: 3
Views: 2273
Reputation: 2128
This code works for me:
var parent = element.Parent as UIElement;
Point bottomLeft = element.TranslatePoint(new Point(0, 0), parent);
Point topRight = element.TranslatePoint(new Point(element.ActualWidth, element.ActualHeight), parent);
var renderWidth = topRight.X - bottomLeft.X;
var renderHeight = topRight.Y - bottomLeft.Y;
Upvotes: 4
Reputation: 169150
How I can determine actual width and actual height of control, after applying the transformation?
You need to calculate the width and height yourself. It should be pretty straight-forward though:
double width = MyCanvas.ActualWidth;
double height = MyCanvas.ActualHeight;
ScaleTransform st = MyCanvas.RenderTransform as ScaleTransform;
if(st != null)
{
width *= st.ScaleX;
height *= st.ScaleY;
}
There is no property that will return this size for you. Tranformations don't affect the ActualWidth
and ActualHeight
properties.
Upvotes: 1