Question

I am trying to resolve a number of frequently occurring data binding errors as given below

System.Windows.Data Error: 40 : BindingExpression path error: 'Active' property not found on 'object' ''FrameViewModel' (HashCode=55649279)'. BindingExpression:Path=Active; DataItem='FrameViewModel' (HashCode=55649279); target element is 'ContentControl' (Name='HeaderBorder'); target property is 'NoTarget' (type 'Object')

I have an attached dependency property Active

public bool Active
    {
        get
        {
            return (bool)GetValue(ActiveProperty);
        }
        set
        {
            SetValue(ActiveProperty, value);
        }
    }

    public static readonly DependencyProperty ActiveProperty
       = DependencyProperty.RegisterAttached("Active", typeof(bool), typeof(Card));

The binding is set in theme files in a control template

<ControlTemplate x:Key="FrameHeader" TargetType="ContentControl">
.....
.....
<ControlTemplate.Triggers>
        <DataTrigger Binding="{Binding Path=Active}" Value="True">
            <Setter TargetName="HeaderBackground" Property="Background" Value="{DynamicResource SelectedHeaderBrush}"/>
        </DataTrigger>
        <DataTrigger Binding="{Binding Path=Active}" Value="False">
            <Setter TargetName="HeaderBackground" Property="Background" Value="{DynamicResource HeaderBrush}"/>
        </DataTrigger>
    </ControlTemplate.Triggers>

The control template is used in a content control as shown here (can be seen in the error as well)

<ControlTemplate x:Key="Card" TargetType="{x:Type Button}">
    <ContentControl Template="{DynamicResource FrameTemplate}">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <ContentControl Template="{DynamicResource FrameHeader}" Grid.Row="0" x:Name="HeaderBorder" >
                <Border Style="{DynamicResource BorderTop}">
                    <DockPanel>
                        <ContentPresenter x:Name="UpperLeftContent" 
                                          DockPanel.Dock="Left"
                                          Visibility="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=icush:Frame},    
                                                               Converter={StaticResource WatchListLockoutTimerVisibilityConverter},    
                                                               Path=UpperLeftContent}" 
                                          Content="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=icush:Frame},  
                                                            Converter={StaticResource WatchListLockoutTimerConverter},    
                                                            ConverterParameter={StaticResource LockoutTimerRadius},
                                                            Path=UpperLeftContent}"
                                          />
                        <TextBlock x:Name="Header" 
                                   Style="{DynamicResource Header}" 
                                   Text="{Binding Path=Title}" />
                    </DockPanel>
                </Border>
            </ContentControl>
            <ContentControl Template="{DynamicResource FrameBody}" Grid.Row="1" Content="{TemplateBinding Content}"/>
        </Grid>
    </ContentControl>
</ControlTemplate>

I have spent a lot of time trying to figure out if the DependencyProperty is missing anything or if the binding is set incorrectly but to no avail. The application works as expected but I'm trying to get rid of such binding errors. The error goes away if I remove the binding in XML but that is not the solution I'm looking for. There might be something wrong with the binding.

I have looked at several other posts, read material and book chapters on Dependency Properties and binding and I can't find any obvious mistake. I have even used snoop but didn't get any additional information from that too. Some of the things I've looked at are given below

BindingExpression path error: property not found on 'object'

System.Windows.Data Error: 40 : BindingExpression path error: property not found on object

WPF Error 40 BindingExpression path error: property not found on 'object'

http://wpftutorial.net/DependencyProperties.html

http://rachel53461.wordpress.com/2012/07/14/what-is-this-datacontext-you-speak-of/

Is there a way to determine where a WPF Binding is declared/created?

WPF slow performance - many DataItem=null binding warnings

Any ideas of what could be going wrong and how I can get rid of the error?

Was it helpful?

Solution 2

So the problem here was that for attached properties you need to enclose the attached property in parenthesis, something which was missing in my code. MSDN link, found in the post pointed to by @Rachel, refers to this requirement.

I was also missing the namespace prefix for the card class. After creating the namespace prefix local I changed the code to the following

<DataTrigger Binding="{Binding Path=(local:Card.Active)}" Value="True">
        <Setter TargetName="HeaderBackground" Property="Background" Value="{DynamicResource SelectedHeaderBrush}"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=(local:Card.Active)}" Value="False">
        <Setter TargetName="HeaderBackground" Property="Background" Value="{DynamicResource HeaderBrush}"/>
</DataTrigger>    

This is all that was needed!

OTHER TIPS

WPF binding errors aren't always the easiest thing to read, but break it up by the colons/semi-colons/periods, and read it backwards:

  • target property is 'NoTarget' (type 'Object')
  • target element is 'ContentControl' (Name='HeaderBorder');
  • DataItem='FrameViewModel' (HashCode=55649279);
  • BindingExpression:Path=Active;
  • 'Active' property not found on 'object' ''FrameViewModel' (HashCode=55649279)'.
  • BindingExpression path error:
  • System.Windows.Data Error: 40 :

This can be read as:

  • Somewhere you are trying to bind a property called NoTarget
  • This property is located on a <ContentControl> with Name="HeaderBorder"
  • The DataItem (DataContext) behind that UI object is of type FrameViewModel
  • The property you are trying to bind is called "Active"
  • The Active property is not found on FrameViewModel
  • The rest of the stuff is usually ignorable

Normally I'd suggest you look in your code for something that looks a bit like this:

<ContentControl Name="HeaderBorder" NoTarget="{Binding Active}" />

and that will be where your problem is for this error message

But, in your case the problem property is NoTarget, which is apparently something special to WPF (I learn something new all the time!) related to the triggers.

A Google quick search returns this post, which suggests the problem is caused by not using the correct syntax for binding to an attached property.

Try writing the binding for your attached property with parenthesis, like this

<DataTrigger Binding="{Binding Path=(local:Card.Active),...

(I'm not entirely sure if this will work because I thought attached dependency properties had to be attached to UI objects, and you have yours attached to a Card object which sounds like a data object. But I could be wrong, or have misunderstood your code :))

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top