Logrus
Topic Author
Posts: 73
Joined: 22 Jul 2015, 12:16

TemplateBinding doesn't work for my UserControl

26 Feb 2025, 11:47

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>
 
User avatar
sfernandez
Site Admin
Posts: 3363
Joined: 22 Dec 2011, 19:20

Re: TemplateBinding doesn't work for my UserControl

28 Feb 2025, 11:27

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.
 
Logrus
Topic Author
Posts: 73
Joined: 22 Jul 2015, 12:16

Re: TemplateBinding doesn't work for my UserControl

28 Feb 2025, 23:51

I see, thanks.

Who is online

Users browsing this forum: No registered users and 7 guests