【问题标题】:Scale child back in WPF在 WPF 中缩放子级
【发布时间】:2023-04-03 01:53:02
【问题描述】:

我有一个Grid,它通过滑块用ScaleTransform 缩放/缩放。在运行时,许多 UIElements 被添加到这个Grid。

我想显示一些工具提示,但没有缩放!我该怎么做?

例如:Grid 有 scaleX 和 scaleY 2,所以我设置了 new ScaleTransform(0.5, 0.5),但没有帮助。似乎最相似的值是 0.740.. 为什么? 甚至Grid 的 LayoutTransform.Inverse 设置为缩放值 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"/>  

代码隐藏: (矩形方法(MouseEnter 事件)动态添加到网格中)

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);
}

【问题讨论】:

    标签: c# wpf transform scale scaling


    【解决方案1】:

    您需要将逆变换分配给子元素,以便子元素保持原样。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-16
      • 1970-01-01
      相关资源
      最近更新 更多