0

我使用一个项目模板来定义我的网格的行必须如何显示。网格定义(简化)显示项目模板源是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 覆盖(或忘记)项目模板源?

非常感谢 !

顺便说一句,我还没有找到这些简单绑定案例的简单指南……如果有人有这样指南的链接,我会永远祝福他/她 :)

4

1 回答 1

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

于 2013-06-14T13:03:02.087 回答