-1

I have a binding failure that does seem to not be a failure, since everything with the binding works like intended. I'm guessing the binding fails but immedietely after succeeds (? but shouldnt the binding failure warning get removed then?).

The binding failure occurs in one of my templates (in a style) for a custom control (ColoredImage). However it only gives a binding failure in that template when that custom control is used inside another template for a custom control (ComPortButton). When i use the ColoredImage control elsewhere, i get no binding failures.

Error reads: System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=Image; DataItem=null; target element is 'ImageBrush' (HashCode=42371273); target property is 'ImageSource' (type 'ImageSource')

When i modify the template from:

<Rectangle x:Name="Icon" Fill="{Binding Path=Color, RelativeSource={RelativeSource TemplatedParent}}" Height="{Binding Path=Height, RelativeSource={RelativeSource TemplatedParent}}" Width="{Binding Path=Width, RelativeSource={RelativeSource TemplatedParent}}" VerticalAlignment="Top">
  <Rectangle.OpacityMask>
     <ImageBrush ImageSource="{Binding Path=Image, RelativeSource={RelativeSource TemplatedParent}}"/>
  </Rectangle.OpacityMask>

to:

<Image Source="{Binding Path=Image, RelativeSource={RelativeSource TemplatedParent}}" x:Name="Icon" Height="{Binding Path=Height, RelativeSource={RelativeSource TemplatedParent}}" Width="{Binding Path=Width, RelativeSource={RelativeSource TemplatedParent}}" VerticalAlignment="Top"/>

Then i get no binding failure, which is odd since the same bindings is used. As a reminder, using this control (coloredimage) gives no binding failures in any other place. Only when i use it from another template.

How can i fix this? The code works like intended, i'm just sick of having the "binding failure" icon glow red all the time.

Both Styles+template for ColoredImage and ComPortButton are in Generic.xaml. Does this have anything to do with it? I tried chaning the order in which i define these styles within Generic.xaml, but it had no effect.

  • Try setting a random `FallbackValue` like `ImageSource="{Binding Path=Image, FallbackValue=0, RelativeSource={RelativeSource TemplatedParent}}"/>`. – Jesse Good Dec 21 '21 at 12:17
  • ah, ofc, don't know why i didnt think of that. That fixed it thanks. –  Dec 23 '21 at 08:55

1 Answers1

0

I think, your problem is related to the difference between Image and ImageBrush classes. The first inherits from the FrameworkElement class but not the second one.

The behavior of your RelativeSource TemplatedParent depends on the template container see: TemplatedParent remark.

The FrameworkElement class contains a DataContext and TemplatedParent Properties used by the Binding system and visualTree.

WPF Elements are hierarchically related with two trees LogicalTree and VisualTree (take a look : Logical Tree and Visual Tree in WPF) .

If your element is not a part of the VisualTree you will get this error.

For Mode details, take a look on this reponse WPF Error: Cannot find governing FrameworkElement for target element

Dharman
  • 30,962
  • 25
  • 85
  • 135
G. Sofien
  • 234
  • 3
  • 11