1

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

2 回答 2

1

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

<KeyButton Content="Hello"/>

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

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

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

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

于 2012-12-21T15:15:41.527 回答
0

您仍然可以采用 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>
于 2012-12-21T14:22:39.880 回答