如何覆盖为ItemTemplate定义的ItemsSource?

时间:2013-06-14 12:55:19

标签: wpf binding itemtemplate

我使用项模板来定义必须如何显示网格行。网格定义(简化)显示项目模板源是GridRows(行的集合):

<grid ...>
   (...)
   <ScrollViewer 
          ItemTemplate="{StaticResource GridRowItemDataTemplate}" 
          ItemsSource="{Binding GridRows}" />
   </ScrollViewer>
</grid>

到目前为止,非常好。

在项目模板中,文本框绑定到ImportZoneName,当然已解决为GridRows[i].ImportZoneName,这正是我想要的:

<DataTemplate x:Key="GridRowItemDataTemplate">
   <Grid>
      <TextBlock {Binding ImportZoneName}" />
      <ComboBox 
           SelectedItem="{Binding SelectedModelingTypeValue}" 
           ItemsSource="{Binding ModelingTypes}" />
   </Grid>
</DataTemplate>

现在出现问题:我还想将组合框绑定到我的视图模型的其他属性(ModelingTypes)。此属性未以任何方式链接到GridRows。如何告诉WPF覆盖(或忘记)项目模板源?

很多,非常感谢!

顺便说一下,我还没有找到这些简单绑定案例的简单指南......如果有人有这样一个指南的链接,我将永远保佑他/她:)

1 个答案:

答案 0 :(得分:2)

您可以像这样获取父列表的DataContext

<DataTemplate x:Key="GridRowItemDataTemplate">
   <Grid>
      <TextBlock {Binding ImportZoneName}" />
      <ComboBox 
           SelectedItem="{Binding SelectedModelingTypeValue}" 
           ItemsSource="{Binding DataContext.ModelingTypes, 
                           RelativeSource={RelativeSource FindAncestor,
                           AncestorType=Grid}}" />
   </Grid>
</DataTemplate>

Grid替换为您正在使用的网格类型(不确定它是什么,在问题中不明显),只要其DataContext具有属性ModelingTypes < / p>

相关问题