C# WPF 如何更改 RenderTransformOrigin 但保持位置?

标签 c# wpf matrix transform rendertransform

我想知道如何在不改变元素位置的情况下改变元素的 RenderTransformOrigin。我注意到更改 RenderTransformOrigin 会影响元素位置。

如何计算更改 RenderTransformOrigin 前后元素位置之间的 XY 值?

XAML

<Button 
   Content="Button" 
   Height="100" 
   Width="100" 
   RenderTransformOrigin="0, 0"> 
          <Button.RenderTransform> 
                 <TransformGroup> 
                        <ScaleTransform/> 
                        <SkewTransform/> 
                        <RotateTransform Angle="90"/> 
                        <TranslateTransform/> 
                 </TransformGroup> 
          </Button.RenderTransform> 
   </Button

最佳答案

RenderTransformOrigin

MSDN says, Gets or sets the center point of any possible render transform declared by System.Windows.UIElement.RenderTransform, relative to the bounds of the element. This is a dependency property.

所以肯定不会对位置有任何影响。位置可能会受到影响 TranslateTransform

<TranslateTransform  X="{Binding LX}" Y="{Binding LY}" />


<Button Content="Hello" Width="100" Height="100" RenderTransformOrigin=".5,.5" BorderThickness="2" BorderBrush="Black">
    <Button.LayoutTransform>
        <TransformGroup>
            <ScaleTransform 
                            ScaleX="{Binding X}" 
                            ScaleY="{Binding Y}"/>
            <RotateTransform 
                            Angle="{Binding RotateAngle}"/>
        </TransformGroup>
    </Button.LayoutTransform>
</Button>

将 X 和 Y 设置为 1 即:X = Y = 1; 在您的 ViewModel 中

我试过这对改变 RenderTransformOrigin 的位置没有影响

MSDN

RenderTransformOrigin has a somewhat nonstandard use of the Point structure value, in that the Point does not represent an absolute location in a coordinate system. Instead, values between 0 and 1 are interpreted as a factor for the range of the current element in each x,y axis. For example, (0.5,0.5) will cause the render transform to be centered on the element, or (1,1) would place the render transform at the bottom right corner of the element. NaN is not an accepted value. Values beyond 0 and 1 are also accepted, and will result in more unconventional transform effects. For instance, if you set RenderTransformOrigin to be (5,5), and then apply a RotateTransform, the rotation point will be well outside the bounds of the element itself. The transform will spin your element around in a big circle that originates beyond bottom right. The origin might be somewhere inside its parent element and could possibly be possibly out of frame or view.

Negative point values are similar, these will go beyond the top left bounds. Render transforms do not affect layout, and are typically used to animate or apply a temporary effect to an element.

关于C# WPF 如何更改 RenderTransformOrigin 但保持位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32113846/

相关文章:

c# - 什么先来——命令还是事件处理程序?

c# - 关于 Java 客户端调用 WCF

c - 未正确读取矩阵

matlab - 使用 MATLAB 在图中查找邻居的邻居

c# - 是否可以在针对 4.7.1 的 WinForms 项目中使用 Path.GetRelativePath (.NET Core2)?

c# - 引用 Razor 类库时重复的基本路径

wpf - F# Async.SwitchToContext 与 Dispatcher.Invoke

c# - 我怎么能在第一个位置有一个空项目的十进制组合框?

wpf - 在 WPF 自定义控件中处理托管资源

java - 以矩阵形式打印二维数组