У меня есть UserControl, который содержит ListBox, который связан с CollectionViewSource с именем Data, и каждый элемент в этой коллекции отображается в ListBox с помощью ItemTemplate. Этот ItemTemplate является ItemsControl, который связан с другим CollectionViewSource с именем Rows. Rows хранит один или несколько объектов MyListBoxRow. Для каждого объекта в Data CollectionViewSource я получаю ListBoxItem, который состоит из ContentPresenters из Rows CollectionViewSource.
Причина, по которой я это делаю, заключается в том, что я могу манипулировать коллекцией Rows во время выполнения и добавлять / удалять «строки» информации.
Проблема, с которой я сталкиваюсь, связана с привязкой данных внутри DataTemplates «NumberDescriptionDataTemplate», «NotesDataTemplate» и «AuditDataTemplate». Например, {Binding Notes} внутри NotesDataTemplate не работает, потому что текущий привязываемый элемент взят из строк, а не данных. Если я изменяю NotesDataTemplate на {Binding Description}, я получаю описание из объекта MyListBoxRow, как и ожидалось.
Как мне изменить оператор Binding в моих шаблонах данных, чтобы информация связывалась с элементом из коллекции данных, а не с элементом из коллекции строк?
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» используется ...
<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>