Composite Collection binding problem [modified]
-
EDIT: A workaround for this issue could be changing the question to : How do I add a default item to my databound listbox? Hello, I have the following code:
<ListBox>
<ListBox.ItemsSource>
<CompositeCollection>
<ListBoxItem>
<StackPanel>
<Image Source="Resources\nottagged.png" Height="15" Width="14"/>
<TextBlock Margin="5,0">Not tagged</TextBlock>
</StackPanel>
</ListBoxItem>
<CollectionContainer Collection="{Binding Path=Manager.Tags}" />
</CompositeCollection>
</ListBox.ItemsSource>
</ListBox>This doesn't work, in the output it writes:
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=Manager.Tags; DataItem=null; target element is 'CollectionContainer' (HashCode=30880833); target property is 'Collection' (type 'IEnumerable')
There is nothing wrong with the binding because this does work (only i miss my custom ListBoxItem, that is not contained in the collection):<ListBox ItemsSource="{Binding Path=Manager.Tags}" />
Binding it to an array of Tag object defined in the resources does work, but of course is not what I need :
<x:Array x:Key="data2" Type="{x:Type data:Tag}" >
<data:Tag Name="Tag1"/>
<data:Tag Name="Tag2"/>
<data:Tag Name="Tag3"/>
</x:Array>with the following replacement in the code sample above:
<CollectionContainer Collection="{StaticResource data2}" />
I did some searches on the internet, found some posts, but they were quite old and vague. Does somebody have a solution for this? Thanks al lot, Gidon
modified on Sunday, May 18, 2008 9:49 AM
-
EDIT: A workaround for this issue could be changing the question to : How do I add a default item to my databound listbox? Hello, I have the following code:
<ListBox>
<ListBox.ItemsSource>
<CompositeCollection>
<ListBoxItem>
<StackPanel>
<Image Source="Resources\nottagged.png" Height="15" Width="14"/>
<TextBlock Margin="5,0">Not tagged</TextBlock>
</StackPanel>
</ListBoxItem>
<CollectionContainer Collection="{Binding Path=Manager.Tags}" />
</CompositeCollection>
</ListBox.ItemsSource>
</ListBox>This doesn't work, in the output it writes:
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=Manager.Tags; DataItem=null; target element is 'CollectionContainer' (HashCode=30880833); target property is 'Collection' (type 'IEnumerable')
There is nothing wrong with the binding because this does work (only i miss my custom ListBoxItem, that is not contained in the collection):<ListBox ItemsSource="{Binding Path=Manager.Tags}" />
Binding it to an array of Tag object defined in the resources does work, but of course is not what I need :
<x:Array x:Key="data2" Type="{x:Type data:Tag}" >
<data:Tag Name="Tag1"/>
<data:Tag Name="Tag2"/>
<data:Tag Name="Tag3"/>
</x:Array>with the following replacement in the code sample above:
<CollectionContainer Collection="{StaticResource data2}" />
I did some searches on the internet, found some posts, but they were quite old and vague. Does somebody have a solution for this? Thanks al lot, Gidon
modified on Sunday, May 18, 2008 9:49 AM