Page 1 of 1

RelativeSource TemplatedParent does not work in Noesis.

Posted: 20 Jul 2020, 17:49
by asusralis
Hello! I'm writing a nine slice control, and I'm having trouble binding my imagebrushes to my template. ImageSource is a custom dependency property. This binds correctly in WPF but not Noesis (it just shows the ImageBrush's ImageSources as null):
<Grid.Resources>
                            <Style TargetType="Rectangle">
                                <Setter Property="SnapsToDevicePixels" Value="True" />
                                <Setter Property="RenderOptions.BitmapScalingMode" Value="NearestNeighbor" />
                            </Style>
                            <ImageBrush
                                x:Key="TLeft"
                                ImageSource="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ImageSource}"
                                ViewboxUnits="Absolute" />
                            <ImageBrush
                                x:Key="T"
                                ImageSource="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ImageSource}"
                                ViewboxUnits="Absolute" />
                            <ImageBrush
                                x:Key="TRight"
                                ImageSource="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ImageSource}"
                                ViewboxUnits="Absolute" />
                            <ImageBrush
                                x:Key="CLeft"
                                ImageSource="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ImageSource}"
                                ViewboxUnits="Absolute" />
                            <ImageBrush
                                x:Key="C"
                                ImageSource="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ImageSource}"
                                ViewboxUnits="Absolute" />
                            <ImageBrush
                                x:Key="CRight"
                                ImageSource="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ImageSource}"
                                ViewboxUnits="Absolute" />
                            <ImageBrush
                                x:Key="BLeft"
                                ImageSource="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ImageSource}"
                                ViewboxUnits="Absolute" />
                            <ImageBrush
                                x:Key="B"
                                ImageSource="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ImageSource}"
                                ViewboxUnits="Absolute" />
                            <ImageBrush
                                x:Key="BRight"
                                ImageSource="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ImageSource}"
                                ViewboxUnits="Absolute" />
                        </Grid.Resources>
Oddly enough, using a simple TemplateBinding didn't work in WPF or Noesis. Is there a reason why you can't use TemplateBinding here? Naming my ControlTemplate and using ElementName also didn't work. As a workaround I manually set the ImageSources in my custom control. I attached the WPF project that has this working.

Re: RelativeSource TemplatedParent does not work in Noesis.

Posted: 24 Jul 2020, 10:53
by sfernandez
The problem is you are using a resource that is defined for the ControlTemplate, and the Binding is being resolved against the ControlTemplate, which doesn't have a TemplatedParent.
This doesn't work in WPF either because it uses the same resource for all the instances of the control, so it can't have different values for each control.

The solution is to define the ImageBrush resources in the visual tree of the template, so each instance of the control gets its own instance of the image brush: