Search code examples
c#wpftransformscalescaling

Scale child back in WPF


I have a Grid which scaled/zoomed with ScaleTransform by slider. At runtime many UIElements are added to this Grid.

I want to show some tooltips, but not scaled! How should I do that?

For the example: Grid has scaleX and scaleY 2, so I set new ScaleTransform(0.5, 0.5), but didn't help. It seems that the most similar value is 0.740.. Why? Even Grid's LayoutTransform.Inverse is set to scale values 0.5.

XAML:

<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Height="Auto" Width="Auto" Name="graphScrollViewer" ScrollChanged="graphScrollViewer_ScrollChanged">
<Grid Margin="0,0,0,0" Name="graphGrid" Width="Auto" Height="Auto" ScrollViewer.IsDeferredScrollingEnabled="True"  MouseLeftButtonDown="graphGrid_MouseLeftButtonDown" MouseLeftButtonUp="graphGrid_MouseLeftButtonUp" MouseMove="graphGrid_MouseMove">
    <Grid.LayoutTransform>
        <ScaleTransform ScaleX="{Binding ElementName=sldZoom, Path=Value}" ScaleY="{Binding ElementName=sldZoom, Path=Value}" />
    </Grid.LayoutTransform>
</Grid>
</ScrollViewer>

<Slider Minimum="0.1" Maximum="20" Value="1" x:Name="sldZoom" Panel.ZIndex="10" Orientation="Horizontal" VerticalAlignment="Bottom" HorizontalAlignment="Right" Margin="0,0,20,20" Height="23" Width="100"  ValueChanged="sldZoom_ValueChanged"/>  

Code-behind: (method of Rectangle (MouseEnter event) dynamically added to grid)

private void rect_MouseEnter(object sender, MouseEventArgs e)
{
    RectToolTip = new TextBlock();
    RectToolTip.HorizontalAlignment = HorizontalAlignment.Left;
    RectToolTip.VerticalAlignment = VerticalAlignment.Top;
    RectToolTip.TextAlignment = TextAlignment.Center;
    RectToolTip.Height = this.HeaderTwoHeight + 1;
    RectToolTip.Text = " " + (RectsTasks[(sender as Rectangle)]).Info + " ";
    RectToolTip.Background = this.ToolTipBackground;
    RectToolTip.Foreground = this.ToolTipFontColor;

    RectToolTipBorder = new Border();
    RectToolTipBorder.Child = RectToolTip;
    RectToolTipBorder.BorderThickness = new Thickness(this.ToolTipBorderThickness);
    RectToolTipBorder.BorderBrush = this.ToolTipBorderColor;
    RectToolTipBorder.Margin = new Thickness(e.GetPosition((graphGrid)).X + 10,     e.GetPosition((graphGrid)).Y + 10, 0, 0);
    RectToolTipBorder.VerticalAlignment = System.Windows.VerticalAlignment.Top;
    RectToolTipBorder.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;

    graphGrid.Children.Add(RectToolTipBorder);

    RectToolTipBorder.LayoutTransform = RectToolTip.LayoutTransform = new ScaleTransform(????);           
    Grid.SetZIndex(RectToolTip, 20);
    Grid.SetZIndex(RectToolTipBorder, 20);
}

Solution

  • You need to assign the inverse transform to the child element, so that the child will stay intact.

    RectToolTipBorder.LayoutTransform = graphGrid.LayoutTransform.Inverse as Transform;