I have a UserControl that contains a ListBox that is bound to a CollectionViewSource called Data and each item in that collection is displayed in the ListBox using an ItemTemplate. That ItemTemplate is an ItemsControl that is bound to another CollectionViewSource called Rows. Rows stores one or more MyListBoxRow objects. For each object in the Data CollectionViewSource, I get a ListBoxItem which is comprised of ContentPresenters from the Rows CollectionViewSource.
The reason I’m doing this is so I can manipulate the Rows collection at run-time and add/remove “rows” of information.
The problem I’m encountering is with the data binding inside the “NumberDescriptionDataTemplate”, “NotesDataTemplate” and “AuditDataTemplate” DataTemplates. For example, {Binding Notes} inside the NotesDataTemplate doesn’t work because the current item being bound is from Rows, not Data. If I change the NotesDataTemplate to {Binding Description} I get the Description from the MyListBoxRow object as expected.
How do I modify the Binding statement in my DataTemplates so the information is bound to an item from the Data collection instead of an item from the Rows collection?
MyListBox.xaml…
<UserControl x:Name="MyListBox"...>
<UserControl.Resources>
<CollectionViewSource x:Key="Data" Source="{Binding ItemsSource, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" />
<CollectionViewSource x:Key="Rows" Source="{Binding ListRows, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" Filter="Rows_Filter" />
</UserControl.Resources>
<ListBox ItemsSource="{Binding Source={StaticResource Data}}">
<ListBox.ItemTemplate>
<DataTemplate>
<ItemsControl ItemsSource="{Binding Source={StaticResource Rows}}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<ContentPresenter ContentTemplate="{Binding RowTemplate}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</UserControl>
“MyListBox” in use…
<Window...>
<Window.Resources>
<DataTemplate x:Key="NumberDescriptionDataTemplate">
<TextBlock Text="{Binding Number_Description}" FontSize="20" />
</DataTemplate>
<DataTemplate x:Key="NotesDataTemplate">
<TextBlock Text="{Binding Description}" />
</DataTemplate>
<DataTemplate x:Key="AuditDataTemplate">
<TextBlock FontSize="8pt" FontStyle="Italic" TextTrimming="CharacterEllipsis">
<TextBlock.Text>
<MultiBinding StringFormat="{}Added On {0:ddd MMM dd, yyyy hh:mm:ss}; Last Modified On {1:ddd MMM dd, yyyy hh:mm:ss}; Removed On {2:ddd MMM dd, yyyy hh:mm:ss}">
<Binding Path="AddedOn" FallbackValue="[Added On]" TargetNullValue="n/a" />
<Binding Path="ModifiedOn" FallbackValue="[Modified On]" TargetNullValue="n/a" />
<Binding Path="RemovedOn" FallbackValue="[Removed On]" TargetNullValue="n/a" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</Window.Resources>
<local:MyListBox ItemsSource="{Binding Samples}">
<local:MyListBox.ListRows>
<local:MyListBoxRow Description="Number, Description"
IsDisplayed="True"
IsRequired="True"
RowTemplate="{StaticResource NumberDescriptionDataTemplate}" />
<local:MyListBoxRow Description="Notes"
IsDisplayed="True"
IsRequired="False"
RowTemplate="{StaticResource NotesDataTemplate}" />
<local:MyListBoxRow Description="Added, Modified, Removed"
IsDisplayed="True"
IsRequired="False"
RowTemplate="{StaticResource AuditDataTemplate}" />
</local:MyListBox.ListRows>
</local:MyListBox>
</Window>
You can use other sources than the DataContext if that is not what you need at that point. When working with ItemsControls you often may want to access the DataContext of said control rather than the item that is being templated, for that you can use a RelativeSource-Binding, e.g.
That is an example, not quite sure i got the whole of your construction, you might need to adapt it.