如何解决与嵌套在ListBox内的ItemsControl一起使用时,WPF DataTemplate绑定不起作用
|| 我有一个UserControl,其中包含一个ListBox,该ListBox绑定到名为Data的CollectionViewSource上,并且使用ItemTemplate在ListBox中显示该集合中的每个项目。该ItemTemplate是一个ItemsControl,绑定到另一个称为Rows的CollectionViewSource。行存储一个或多个MyListBoxRow对象。对于数据CollectionViewSource中的每个对象,我都会得到一个ListBoxItem,它由Rows CollectionViewSource中的ContentPresenters组成。 我这样做的原因是,我可以在运行时操作Rows集合并添加/删除“行”信息。 我遇到的问题是\“ NumberDescriptionDataTemplate \”,\“ NotesDataTemplate \”和\“ AuditDataTemplate \” DataTemplates内部的数据绑定。例如,NotesDataTemplate中的{Binding Notes}不起作用,因为要绑定的当前项来自行而不是数据。如果将NotesDataTemplate更改为{Binding Description},则会按预期从MyListBoxRow对象获取描述。 如何修改DataTemplates中的Binding语句,以便将信息绑定到Data集合中的项目,而不是Rows集合中的项目? 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,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>
解决方法
如果那时候您不需要DataContext,则可以使用其他来源。当使用ItemsControls时,您通常可能希望访问所述控件的DataContext,而不是要被模板化的项目,为此,您可以使用RelativeSource-Binding,例如
{Binding RelativeSource={RelativeSource AncestorType=ListBox},Path=DataContext.Data}
这是一个例子,不太确定我是否掌握了整个结构,您可能需要对其进行调整。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。