wpf - 为什么这个 RotateTransform 不能以编程方式工作?

标签 wpf rotatetransform

我想创建一个允许其内容旋转的按钮控件。我的类(class)看起来像这样。 ApplyRotation 确实被调用,但文本保持水平。

我是不是忘记了什么?无效布局或类似的东西...

public class KeyButton : Button
{
    private TextBlock content;
    private Viewbox viewbox;

    #region DEPENDENCY - Angle
    // Dependency Property
    public static readonly DependencyProperty AngleProperty =
         DependencyProperty.Register("Angle", typeof(double),
            typeof(KeyButton),
            new FrameworkPropertyMetadata(0.0, OnAnglePropertyChanged, OnCoerceAngleProperty),
          OnValidateAngleProperty);

    // .NET Property wrapper
    public double Angle
    {
        get { return (double)GetValue(AngleProperty); }
        set { SetValue(AngleProperty, value); }
    }

    private static void OnAnglePropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
    {
        KeyButton control = source as KeyButton;
        if (control != null)
        {
            ApplyRotation(control);
        }
    }

    private static object OnCoerceAngleProperty(DependencyObject sender, object data)
    {
        // TODO implement
        return data;
    }

    private static bool OnValidateAngleProperty(object data)
    {
        // TODO implement validation
        return data is double;
    }
    #endregion

    public KeyButton()
    {
        viewbox = new Viewbox();
        content = new TextBlock { Text = "X" };
        this.Content = viewbox;
        viewbox.Child = content;
    }

    private static void ApplyRotation(KeyButton control)
    {
        if (control.viewbox.Child != null)
        {
            control.viewbox.Child.RenderTransform = new RotateTransform(control.Angle);
        }
    }

    protected override void OnPropertyChanged(System.Windows.DependencyPropertyChangedEventArgs e)
    {

        base.OnPropertyChanged(e);
        switch (e.Property.Name)
        {
            case "Content":
                content.Text = e.NewValue.ToString();
                ApplyRotation(this);
                break;
            default:
                try
                {
                    viewbox.SetValue(e.Property, e.NewValue);
                }
                catch (Exception)
                {
                }
                break;
        }
    }
}

我尝试使用 XAML,效果很好。 (我有大约 100 个按钮,所以我认为创建一个新类会更好...)

<Button Grid.Row="0" Grid.Column="0">
    <Viewbox>
        <TextBlock Text="Hello">
            <TextBlock.RenderTransform>
                <RotateTransform Angle="45"/>
            </TextBlock.RenderTransform>
        </TextBlock>
    </Viewbox>
</Button>

最佳答案

问题是当您的 KeyButton 的 Content 属性在 XAML 中设置如下时

<KeyButton Content="Hello"/>

您在构造函数中分配的 Viewbox 将被新的内容对象替换。您重写的 OnPropertyChanged 方法不会阻止这种情况。您的 viewbox 成员将不再包含在 Content 属性和分配中

control.viewbox.Child.RenderTransform = new RotateTransform(...)

没有可见的效果,因为 control.viewbox 不再可见。

只要您的所有按钮具有相同的旋转角度,我强烈建议按照 Erno 所示的默认按钮样式修改 ContentTemplate。

关于wpf - 为什么这个 RotateTransform 不能以编程方式工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13991570/

相关文章:

c# - 如何将我的 wpf 应用程序连接到在线数据库?

c# - 保存用户设置的最佳时间

wpf - 依赖属性 - 如何添加所有者以使其充当附加属性?

css - 使用 CSS3 转换将 DIV 放在一边

javascript - 使用 Javascript/jQuery 获取 rotate3d 值

javascript - jQuery - rotateY 取决于元素向右或向左悬停

java - 旋转矩形并以正弦波方式移动它 - 使用graphics2D帮助

.net - 如何将 WebBrowser 显示置于我的控制之下?

c++ - 使用 OpenCV 2.4.3 的双线性插值的 VideoCapture 旋转

wpf - 如何在 WPF UI 代码中等待后台线程/操作完成?