How to bind a property in the context of these ancestors?

I have an ItemControl as shown below. Basically, I have a list of editors (EditorList), and I need hyperlinks for each. However, the open command property (OpenEditorCommand) is at the same level as the list of editors. How can I refer to this property when the context is set to an item in the list. I tried working with the RelativeSource method, but it is too confusing for me to understand. Am I on the right track?

<ItemsControl ItemsSource="{Binding EditorList}">
   <ItemsControl.ItemTemplate>
      <DataTemplate>
         <TextBlock Margin="2,6" HorizontalAlignment="Center">
            <Hyperlink Command="{Binding RelativeSource={RelativeSource AncestorType=ItemsControl, AncestorLevel=2, Mode=FindAncestor}, Path=OpenEditorCommand}" CommandParameter="{Binding Name}">
               <StackPanel>
                  <Image Source="{Binding Image}" Width=32/>
                  <TextBlock Text="{Binding Path=Name}"/>
               </StackPanel>
            </Hyperlink>
         </TextBlock>
      </DataTemplate>
   </ItemsControl.ItemTemplate>
</ItemsControl>
+3
source share
1 answer

You only need to set it AncestorLevelin cases when in the tree of elements there are possibly more than one ancestor of the desired type. The default value is 1, which means finding the nearest one.

But you need to specify in Pathwhich you want to bind to OpenEditorCommandin DataContext ItemsControl:

Command="{Binding Path=DataContext.OpenEditorCommand, RelativeSource={RelativeSource AncestorType=ItemsControl, Mode=FindAncestor}}"
+4
source

All Articles