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

时间:2012-12-21 14:03:10

标签: wpf rotatetransform

我想创建一个按钮控件,允许旋转其内容。我的班级看起来像这样。调用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>

2 个答案:

答案 0 :(得分:1)

你仍然可以采用XAML方式:

<Style TargetType="Button">
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <Viewbox>
                    <ContentPresenter>
                        <ContentPresenter.RenderTransform>
                            <RotateTransform Angle="45"/>
                        </ContentPresenter.RenderTransform>
                    </ContentPresenter>
                </Viewbox>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

答案 1 :(得分:1)

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

<KeyButton Content="Hello"/>

您在构造函数中指定的Viewbox将替换为新的内容对象。您重写的OnPropertyChanged方法不会阻止这种情况。然后,您的viewbox成员不再包含在内容属性和作业

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

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

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