【问题标题】:Why does this RotateTransform not work programmatically?为什么这个 RotateTransform 不能以编程方式工作?
【发布时间】:2012-12-21 14:03:09
【问题描述】:

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

【问题讨论】:

  • 所有 Button 的 Rotation 是否不同,还是在运行时发生变化?
  • 我只对一个按钮应用了一个角度,它没有效果。 set 属性方法仅以新角度(45°)调用一次,但文本保持水平...

标签: wpf rotatetransform


【解决方案1】:

问题在于,当您的 KeyButton 的 Content 属性在 XAML 中设置时,如下所示

<KeyButton Content="Hello"/>

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

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

由于control.viewbox 不再可见,因此没有可见效果。

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

【讨论】:

    【解决方案2】:

    您仍然可以采用 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>
    

    【讨论】:

    • 当然 - 我知道它有效。我只是想为自己节省一些打字时间,并打开通过类向控件添加更多内容的可能性。正如我所说,有很多按钮......
    • @paul 你只需要用按钮的默认样式写一次。
    • 但是如果有人在按钮中放一张图片怎么办...这就是为什么你应该使用 ContentPresenter
    • 但是你不能在运行时改变它,这不符合重点,不是吗?
    • @austinwbryan 这不是要求,要求是节省打字。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-18
    • 2013-03-04
    • 1970-01-01
    • 2016-01-31
    相关资源
    最近更新 更多