WPF UserControl in DataTemplate within ItemsControl - how to bind to parent of ItemsSource

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-01 03:58:01

I have found an answer to my own question, which I hope will help others. The working syntax I have is this:

<StackPanel>
    <ItemsControl ItemsSource="{Binding Path=FullnameList}">
    ...then...
    <ItemsControl.ItemTemplate>
      <DataTemplate>
        <jasControls:NameView
          NameValue="{Binding Path=.}" 
          InEditMode= "{Binding DataContext.ParentInEditMode,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type StackPanel}}}" />

This correctly picks up the property that is a sibling of FullnameList and passes it to the data template item. More by luck than judgement, but I hope this is a valid way to do this!

For each Item in ItemsSource, ItemsControl creates the specified DataTemplate and to its DataContext it assigns the respective Item. Now every DataTemplate can bind to its item in its data context.

So I suppose your item does have a property "ParentInEditMode"; there should be no issue with binding to that property.

If it doesn't work, please update your question with some code.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!