我有一个列表(见下文)包含在一个窗口。窗口的DataContext有两个属性,Items和AllowItemCommand。

我如何获得绑定的超级链接的命令属性需要针对窗口的数据文本解析?

<ListView ItemsSource="{Binding Items}">
  <ListView.View>
    <GridView>
      <GridViewColumn Header="Action">
        <GridViewColumn.CellTemplate>
          <DataTemplate>
            <StackPanel>
              <TextBlock>

                <!-- this binding is not working -->
                <Hyperlink Command="{Binding AllowItemCommand}"
                           CommandParameter="{Binding .}">
                    <TextBlock Text="Allow" />
                </Hyperlink>

              </TextBlock>
            </StackPanel>
          </DataTemplate>
        </GridViewColumn.CellTemplate>
      </GridViewColumn>
    </GridView>
  </ListView.View>
</ListView>

当前回答

你可以尝试这样做:

...Binding="{Binding RelativeSource={RelativeSource FindAncestor, 
AncestorType={x:Type Window}}, Path=DataContext.AllowItemCommand}" ...

其他回答

你可以尝试这样做:

...Binding="{Binding RelativeSource={RelativeSource FindAncestor, 
AncestorType={x:Type Window}}, Path=DataContext.AllowItemCommand}" ...

这也适用于Silverlight 5(也许更早,但我还没有测试它)。我使用了像这样的相对源,它工作得很好。

RelativeSource="{RelativeSource Mode= find祖宗,祖宗类型=telerik:RadGridView}"

这也可以:

<Hyperlink Command="{Binding RelativeSource={RelativeSource AncestorType=ItemsControl},
                             Path=DataContext.AllowItemCommand}" />

ListView将从Window继承它的DataContext,所以它现在也是可用的。 因为ListView,就像类似的控件(例如Gridview, ListBox等),是ItemsControl的子类,绑定这样的控件将完美地工作。