1

I'm running in a issue while trying to use dependency properties in objects which are parts of a collection, inside acustom control, collection identified with the "ContentProperty" attribute. Ok, that's quite unclear. Here is sample of my custom control :

Here is my custom control basic definition :

[ContentProperty("SmarSearchScopes ")]
public class SmartSearchCc : Control
{
    List<SmartSearchScope> SmarSearchScopes {get;set;}
    (more code here)
}

Here is the basic definition of a SmartSearchScope object :

public class SmartSearchScope : DependencyObject
{
    public static readonly DependencyProperty ViewProperty =DependencyProperty.Register("View", typeof (ICollectionView), typeof (SmartSearchScope),new UIPropertyMetadata(null,OnViewChanged));

    public static readonly DependencyProperty FilterColumnsProperty =DependencyProperty.Register("FilterColumns", typeof (IEnumerable<ColumnBase>), typeof (SmartSearchScope),new UIPropertyMetadata(null, OnFilterColumnsChanged));
    public ICollectionView View
    {
        get { return (ICollectionView) GetValue(ViewProperty); }
        set { SetValue(ViewProperty, value); }
    }

    public IEnumerable<ColumnBase> FilterColumns
    {
        get { return (IEnumerable<ColumnBase>) GetValue(FilterColumnsProperty); }
        set { SetValue(FilterColumnsProperty, value); }
    }
    (more code here)
}

All that for what ? Being able to pass a collection of SmartSearchScope objects via XAML like so :

<SmartSearch:SmartSearchCc HorizontalAlignment="Stretch" Grid.Row="0" >
    <SmartSearch:SmartSearchScope  FilterColumns="{Binding ElementName=CcyPairsConfigBlotter, Path=Columns}" View ="{Binding ElementName=CcyPairsConfigBlotter, Path=ItemsSource}"/>
    <SmartSearch:SmartSearchScope FilterColumns="{Binding ElementName=ClientConfigBlotter, Path=Columns}" View ="{Binding ElementName=ClientConfigBlotter, Path=ItemsSource}"/>
</SmartSearch:SmartSearchCc>

'ClientConfigBlotter' and 'CcyPairsConfigBlotter' are just two ItemsControls which expose a 'Columns' and an 'ItemSource' d-property.

The problem here is that althought my 2 SmartSearchScope objects gets instantiated, the databinding on the "View" and "FilterColumns" d-properties is not made and I never go througth the the associated callbacks.

In addition, here is the output error message I got when creating the custom control.

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=Columns; DataItem=null; target element is 'SmartSearchScope' (HashCode=56862858); target property is 'FilterColumns' (type 'IEnumerable`1')
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=ItemsSource; DataItem=null; target element is 'SmartSearchScope' (HashCode=56862858); target property is 'View' (type 'ICollectionView')

This is obvious that I'm missing something but I can't find what.

I must say that, in a previous version of that control, these 2 problematic d-properties where SmartSearchCc properties and that all worked just fine.

Thanks for your help :)

--bruno

H.B.
  • 166,899
  • 29
  • 327
  • 400
Bruno
  • 1,944
  • 13
  • 22
  • Hi, can you not make SmartSearchScope an ItemsControl? I'd say that would simply things a lot. –  Mar 02 '11 at 10:38

2 Answers2

1

I had a similar problem here: Bindings on child dependency object of usercontrol not working

The reason the binding doesn't work is because DependencyObjects don't have a DataContext property. In my case I changed them to inherit from FrameworkElement which solved the problem.

Although as someone else has mentioned, changing the parent control to an ItemsControl could simplify things.

Community
  • 1
  • 1
David Masters
  • 8,069
  • 2
  • 44
  • 75
  • Hello DavidThx for your answer. Indeed, this is exactly the same problem. I found the "Freezable" thing a bit dirty althought it did works. About setting the datacontext to child object, when would you do that ? – Bruno Mar 02 '11 at 13:16
  • And, btw, I not sure how inheriting from ItemsControl would simplify things. I thought it woud be much more complicated. I'm going to look that way though :) – Bruno Mar 02 '11 at 13:18
  • Hi Bruno - yeah I didn't use the Freezable stuff as I did not require it's functionality - I just wanted to get the bindings to work, so that's why I just used FrameworkElement. I set the datacontext within 'OnApplyTemplate'. I iterated over the child collection and did child.DataContext = this.DataContext; – David Masters Mar 02 '11 at 13:44
  • Btw - I didn't have change the collection type.. just literally inherit from FrameworkElement instead of DependencyObject – David Masters Mar 02 '11 at 13:52
0

Ok, problem solved, I swithc inheritance of my main custom control from control to ItemsControl and inheritance of my child object to FrameWork element and that's it. no need to further modifications.

Thank you all for your suggestions !

Bruno
  • 1,944
  • 13
  • 22