Wpf 为什么这个RotateTransform不能以编程方式工作?
我想创建一个按钮控件,允许其内容旋转。我的班级看起来像这样。会调用ApplyRotation,但文本保持水平 我忘了什么吗?使布局无效或类似Wpf 为什么这个RotateTransform不能以编程方式工作?,wpf,rotatetransform,Wpf,Rotatetransform,我想创建一个按钮控件,允许其内容旋转。我的班级看起来像这样。会调用ApplyRotation,但文本保持水平 我忘了什么吗?使布局无效或类似 public class KeyButton : Button { private TextBlock content; private Viewbox viewbox; #region DEPENDENCY - Angle // Dependency Property public static readonly
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个按钮,所以我想最好创建一个新类…)
您仍然可以采用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>
您仍然可以采用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>
问题在于,当在XAML中设置按键的内容属性时,如下所示
<KeyButton Content="Hello"/>
自控件以来没有可见效果。viewbox
不再可见
只要所有按钮的旋转角度相同,我强烈建议您以默认按钮样式修改ContentTemplate,如Erno所示。问题在于,当您的KeyButton的内容属性在XAML中设置时,如下图所示
<KeyButton Content="Hello"/>
自控件以来没有可见效果。viewbox
不再可见
只要所有按钮的旋转角度都相同,我强烈建议您以Erno显示的默认按钮样式修改ContentTemplate。所有按钮的旋转角度都不同,还是在运行时会发生变化?我只对一个按钮应用了角度,但没有任何效果。使用新角度(45°)只调用一次set property方法,但文本保持水平…所有按钮的旋转是否不同,还是在运行期间发生了更改?我只对一个按钮应用了一个角度,没有任何效果。使用新角度(45°)只调用一次set property方法,但文本保持水平…当然-我知道它可以工作。我只是想节省一些打字时间,并打开通过类向控件添加更多内容的可能性。就像我说的,有很多按钮…@paul你只需要写一次,用默认的按钮样式。但是如果有人在按钮中放一个图像怎么办。。。这就是为什么你应该使用ContentPresenter,但你不能在运行时更改它,这违背了这一点,不是吗?@austinwbryan这不是一个要求,要求是保存键入。当然-我知道它是有效的。我只是想节省一些打字时间,并打开通过类向控件添加更多内容的可能性。就像我说的,有很多按钮…@paul你只需要写一次,用默认的按钮样式。但是如果有人在按钮中放一个图像怎么办。。。这就是为什么你应该使用ContentPresenterB,但你不能在运行时更改它,这违背了这一点,不是吗?@austinwbryan这不是一个要求,要求是保存键入。