2011-06-21 38 views
0

我有一个UserControl,它包含绑定到一个名为Data的CollectionViewSource的ListBox,并且该集合中的每个项目都使用ItemTemplate显示在ListBox中。该ItemTemplate是一个ItemsControl,它绑定到另一个名为Rows的CollectionViewSource。行存储一个或多个MyListBoxRow对象。对于Data CollectionViewSource中的每个对象,我得到一个ListBoxItem,它由来自行CollectionViewSource的ContentPresenters组成。WPF DataTemplate Binding与ListBox中嵌套的ItemsControl一起使用时不起作用

我这样做的原因是我可以在运行时操作Rows集合并添加/删除“行”的信息。

我遇到的问题是“NumberDescriptionDataTemplate”,“NotesDataTemplate”和“AuditDataTemplate”DataTemplates中的数据绑定。例如,NotesDataTemplate中的{Binding Notes}不起作用,因为当前绑定的项目来自行而不是数据。如果我将NotesDataTemplate更改为{Binding Description},则按预期方式从MyListBoxRow对象获取Description。

如何修改我的DataTemplates中的绑定语句,以便将信息绑定到数据集合中的项目而不是来自行集合中的项目?

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> 

回答

3

您可以使用其他来源比DataContext的,如果不是,你需要在这一点上有什么。在使用ItemsControls时,您通常可能需要访问所述控件的DataContext,而不是正在模板化的项目,因此您可以使用RelativeSource-Binding(例如,

{Binding RelativeSource={RelativeSource AncestorType=ListBox}, Path=DataContext.Data} 

这是一个例子,搞不清我得到了整个的建筑,你可能需要去适应它。

+0

谢谢。我有使用RelativeSource的经验,但不知道在Path中使用DataContext。我也使用AncestoryType = {x:Type ListBoxItem},所以我可以绑定到“行”数据。 –

相关问题