How to bind to parent DataTemplate from within an ItemsControl.ItemTemplate

点点圈 提交于 2019-12-04 18:07:29

You can use a RelativeSource binding

<ComboBox ItemsSource="{Binding SomeCollection, 
              RelativeSource={RelativeSource 
                  AncestorType={x:Type local:MyContainerControl}}}"/>

What you use for your binding path depends on where the collection is located. If it is located as a DependencyProperty on MyContainerControl, then the above binding works fine. If it is located in the DataContext of MyContainerControl, then you'll need to set the binding path to DataContext.SomeCollection

Maybe use TemplateBinding?

Something like:

{TemplateBinding YourPropertyInTheDataTemplateContext}
Kanji Tattoo

I've always been a big fan of ElementName. Basically you make sure you name the outer level control like : x:Name="MainWin" and then you can do something like this:

<DataTemplate>
        <StackPanel Orientation="Horizontal">
                <ComboBox ItemsSource="{Binding ElementName=MainWin, Path=DataContext.SomeCollection}"/>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!