asusralis
Topic Author
Posts: 85
Joined: 30 Jul 2018, 05:03

RelativeSource TemplatedParent does not work in Noesis.

20 Jul 2020, 17:49

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.
Attachments
WpfApp4.rar
(172.02 KiB) Downloaded 11 times
 
User avatar
sfernandez
Site Admin
Posts: 1915
Joined: 22 Dec 2011, 19:20

Re: RelativeSource TemplatedParent does not work in Noesis.

24 Jul 2020, 10:53

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:

Who is online

Users browsing this forum: Google [Bot] and 1 guest