Page 1 of 1

TemplateBinding doesn't work for my UserControl

Posted: 26 Feb 2025, 11:47
by Logrus
Hi,
Binding + RelativeSource works, but TemplateBinding does not. What could be the reason?
	class ENGINE_API RadialAngleControl final : public Noesis::UserControl
	{
	public:
		RadialAngleControl() = default;

		float GetAngle() const;
		void SetAngle(float value);

		inline static const Noesis::DependencyProperty* AngleProperty;
		NS_DECLARE_REFLECTION(RadialAngleControl, UserControl)
	};
}
	NS_IMPLEMENT_REFLECTION(RadialAngleControl)
	{
		auto data = NsMeta<Noesis::UIElementData>(Noesis::TypeOf<SelfClass>());
		data->RegisterProperty<float>(AngleProperty, "Angle", FrameworkPropertyMetadata::Create(45.f));
	}
    <ControlTemplate x:Key="Template.RadialAngleControl" TargetType="{x:Type RadialAngleControl}">
        <Viewbox>
            <Canvas Width="100" Height="100">

                <Ellipse Width="100" Height="100" />

		<-- Binding OK -->
                <Line X1="50" X2="50" Y1="50" Y2="10">
                    <Line.RenderTransform>
                        <RotateTransform Angle="{Binding Path=Angle, RelativeSource={RelativeSource AncestorType={x:Type RadialAngleControl}}}"  CenterX="50" CenterY="50" />
                    </Line.RenderTransform>
                </Line>
                
               <-- Binding not working --> 
               <Line X1="50" X2="50" Y1="50" Y2="10">
                    <Line.RenderTransform>
                        <RotateTransform Angle="{TemplateBinding Angle}"  CenterX="50" CenterY="50" />
                    </Line.RenderTransform>
                </Line>

            </Canvas>
        </Viewbox>
    </ControlTemplate>

Re: TemplateBinding doesn't work for my UserControl

Posted: 28 Feb 2025, 11:27
by sfernandez
This happens because the RotateTransform doesn't have a TemplatedParent, so TemplateBinding can't find your control's property. In this case you have to use a RelativeSource with AncestorType as you indicated. I have verified this is the same behavior in WPF.

Re: TemplateBinding doesn't work for my UserControl

Posted: 28 Feb 2025, 23:51
by Logrus
I see, thanks.