如何从ItemsControl.ItemTemplate中绑定到父DataTemplate

Bri*_*ett 2 data-binding wpf datatemplate

我有一个容器类型控件,其中包含许多项目.容器控件有一个已DataTemplate定义的,它还包含一个ItemsControl带有a DataTemplate的项目.但是这些项需要绑定到容器控件中的某些内容.下面给出一个简化的例子:

<DataTemplate DataType="{x:Type ContainerType}">

    <!-- Display of the container stuff-->

    <ItemsControl ItemsSource="{Binding Items, Mode=OneWay}">

        <ItemsControl.ItemTemplate>
              <DataTemplate DataType="{x:Type Item}">

                  <!-- Display of the item stuff -->
                  <ComboBox Text="Choose a container-level option..."
                            ItemsSource="{WHAT GOES HERE?}"/>

                </DataTemplate>
         </ItemsControl.ItemTemplate>

      </ItemsControl>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)

如何将项目级别的东西绑定到容器级别?

Rac*_*hel 6

您可以使用RelativeSource绑定

<ComboBox ItemsSource="{Binding SomeCollection, 
              RelativeSource={RelativeSource 
                  AncestorType={x:Type local:MyContainerControl}}}"/>
Run Code Online (Sandbox Code Playgroud)

您用于绑定路径的内容取决于集合的位置.如果它位于DependencyPropertyon MyContainerControl,那么上面的绑定工作正常.如果它位于DataContextof MyContainerControl,则需要将绑定路径设置为DataContext.SomeCollection