我正在尝试创建最简单的Silverlight模板化控件,但我似乎无法让TemplateBinding在RotateTransform的角度属性上工作。
这是来自generic.xaml的ControlTemplate:
<ControlTemplate TargetType="local:CtlKnob">
<Grid x:Name="grid" RenderTransformOrigin="0.5,0.5">
<Grid.RenderTransform>
<TransformGroup>
<RotateTransform Angle="{TemplateBinding Angle}"/> <!-- This does not work -->
<!-- <RotateTransform Angle="70"/> --> <!-- This works -->
</TransformGroup>
</Grid.RenderTransform>
<Ellipse Stroke="#FFB70404" StrokeThickness="19"/>
<Ellipse Stroke="White" StrokeThickness="2" Height="16" VerticalAlignment="Top"
HorizontalAlignment="Center" Width="16" Margin="0,2,0,0"/>
</Grid>
</ControlTemplate>下面是C#:
using System.Windows;
using System.Windows.Controls;
namespace CtlKnob
{
public class CtlKnob : Control
{
public CtlKnob()
{
this.DefaultStyleKey = typeof(CtlKnob);
}
public static readonly DependencyProperty AngleProperty =
DependencyProperty.Register("Angle", typeof(double), typeof(CtlKnob), null);
public double Angle
{
get { return (double)GetValue(AngleProperty); }
set { SetValue(AngleProperty,value); }
}
}
}发布于 2010-01-23 23:02:07
以下是一种解决方法:
public class TemplatedControl1 : Control
{
public TemplatedControl1()
{
this.DefaultStyleKey = typeof(TemplatedControl1);
}
public override void OnApplyTemplate()
{
var transform = this.GetTemplateChild("Transform1") as RotateTransform;
transform.Angle = this.StartAngle;
base.OnApplyTemplate();
}
public static readonly DependencyProperty StartAngleProperty =
DependencyProperty.Register("StartAngle", typeof(double), typeof(TemplatedControl1), null);
public double StartAngle
{
get { return (double)GetValue(StartAngleProperty); }
set { SetValue(StartAngleProperty, value); }
}
}和xaml:
<Style TargetType="local:TemplatedControl1">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:TemplatedControl1">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<Canvas>
<Polyline Fill="Black" >
<Polyline.RenderTransform>
<RotateTransform x:Name="Transform1" />
</Polyline.RenderTransform>
</Polyline>
</Canvas>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>发布于 2010-01-24 06:05:01
Henrik已经准备好了答案,但我将解释为什么它是必要的。
目前在Sliverlight3中,这种绑定要求接收绑定的对象是一个FrameworkElement。它是具有SetBinding方法的FrameworkElement,它允许这些东西工作。
作为DependencyObject的RotateTransform不是FrameworkElement,因此不能参与这种绑定。Silverlight4允许绑定在DependencyObject上工作,所以从理论上讲,这种代码应该可以在SL4上工作。我得试一试,看看这是不是真的。
发布于 2010-01-24 20:15:19
感谢安东尼解释为什么它不起作用。
感谢Henrik的解决方法,它解决了一半的问题:现在可以从Xaml设置角度。然而,它仍然不能以编程的方式设置,但是缺少的部分现在很容易。
下面是完整的解决方案:
public class CtlKnob : Control
{
public CtlKnob()
{
this.DefaultStyleKey = typeof(CtlKnob);
}
public static readonly DependencyProperty AngleProperty =
DependencyProperty.Register("Angle", typeof(double), typeof(CtlKnob),
new PropertyMetadata(AngleChanged));
public double Angle
{
get { return (double)GetValue(AngleProperty); }
set { SetValue(AngleProperty,value); }
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
var transform = this.GetTemplateChild("RotateTransform") as RotateTransform;
transform.Angle = this.Angle;
}
private static void AngleChanged( DependencyObject dobj,
DependencyPropertyChangedEventArgs e )
{
var knob = dobj as CtlKnob;
var rotateTransform = knob.GetTemplateChild("RotateTransform") as
RotateTransform;
if (rotateTransform != null)
rotateTransform.Angle = (double)e.NewValue;
}
}https://stackoverflow.com/questions/2123320
复制相似问题