Cannot find governing FrameworkElement or FrameworkContentElement for target element

Oct 8, 2008 at 7:48 PM
Edited Oct 8, 2008 at 7:52 PM
I am attempting to bind a CollectionViewSource's Source property to the ItemsSource of a DataGrid.  But when I do this I see the following error in the Output window of Visual Studio, and the binding doesn't work:

Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=ItemsSource; DataItem=null; target element is 'CollectionViewSource' (HashCode=49205706); target property is 'Source' (type 'Object')

My XAML looks something like this:

 

<dg:DataGrid x:Name="dgDocumentList" ItemsSource="{Binding Path=BusinessCloseItems}">

 

 

<dg:DataGridTextColumn Width="Auto" Header="Batch" DataFieldBinding="{Binding Path=BatchId}">

 

 

<dg:DataGridTextColumn.HeaderTemplate>

 

 

<DataTemplate>

 

 

<DataTemplate.Resources>

 

 

<CollectionViewSource Source="{Binding ElementName=dgDocumentList, Path=ItemsSource}" x:Key="cvsBatch">

 

 

<CollectionViewSource.GroupDescriptions>

 

 

<PropertyGroupDescription PropertyName="BatchId"/>

 

 

</CollectionViewSource.GroupDescriptions>

 

 

</CollectionViewSource>

 

 

 </DataTemplate.Resources>

 

 

<StackPanel Orientation="Horizontal">

 

 

<TextBlock Text="{Binding}" VerticalAlignment="Center"/>

 

 

<ComboBox ItemsSource="{Binding Source={StaticResource cvsBatch}, Path=Groups}" DisplayMemberPath="Name"/>

 

 

</StackPanel>

 

 

</DataTemplate>

 

 

</dg:DataGridTextColumn.HeaderTemplate>

 

 

</dg:DataGridTextColumn>

 

 

The CollectionViewSource binding works fine if I bind to an element such as ListBox.  It's only when I try to bind to a DataGrid that I get the error.

Any ideas?

Thanks.
-Larry

Oct 8, 2008 at 8:21 PM
Well it seems that if I move my CollectionViewSource out of the DataTemplate resources and into the top-level resources, it works.  I get the same behavior with a ListBox, so it must be XAML-related behavior, and not the DataGrid.

-Larry