3

我定义了一个 ItemsControl,如下所示。基本上,我有一个编辑器列表(EditorList),我想要每个编辑器的超链接。但是,我的打开命令 (OpenEditorCommand) 属性与编辑器列表处于同一级别。当上下文设置为列表中的项目时,我如何引用该属性。我尝试过使用 RelativeSource 方法,但它太复杂了,我无法理解。我在正确的轨道上吗?

<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>
4

1 回答 1

4

您只需要AncestorLevel在元素树中可能存在多个搜索类型的祖先的情况下设置 。默认值为 1,表示找到最接近的。

但是您需要在 中指定Path要绑定到OpenEditorCommand的:DataContextItemsControl

Command="{Binding Path=DataContext.OpenEditorCommand, RelativeSource={RelativeSource AncestorType=ItemsControl, Mode=FindAncestor}}"
于 2012-05-03T21:32:00.367 回答