与嵌套在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, 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>
    
已邀请:
        如果那时候您不需要DataContext,则可以使用其他来源。当使用ItemsControls时,您通常可能希望访问所述控件的DataContext,而不是要被模板化的项目,为此,您可以使用RelativeSource-Binding,例如
{Binding RelativeSource={RelativeSource AncestorType=ListBox}, Path=DataContext.Data}
这是一个例子,不太确定我是否掌握了整个结构,您可能需要对其进行调整。     

要回复问题请先登录注册