绑定到页面的 DataContext

Bind to Page's DataContext

我发现需要绑定到 PageDataContext,设置如下:

<d:Page.DataContext>
    <designTime:PayeesPageDesignViewModel />
</d:Page.DataContext>

在设计时。但是,我需要从具有不同数据上下文的子控件绑定到它:

<GridView x:Name="PayeesGridView"
          Margin="0,30,0,0"
          IsItemClickEnabled="True"
          ItemsSource="{Binding Payees}"
          SelectionChanged="PayeesGridView_OnSelectionChanged">

由于 ItemsSource 是为 GridView 设置的,因此我的 GridViewItemsDataContext 被设置为 Payees 集合。我的 ViewModel 有一个 属性,我需要从转换器中的 View 引用它以确定项目的可见性。

我可以根据 Payee 对象的 属性 设置可见性,如下所示:

<Border Width="250"
        Height="250"
        Background="Gray"
        Opacity="0.85"
        Visibility="{Binding Path=IsOpen,
                     Converter={StaticResource AccountStatusToVisibilityConverter}}">

但我真正需要绑定的是 PageDataContextSettings.ShowInactive 属性。有没有办法从子控件中获取上下文?我使用的是 WinRT,所以我没有 FindAncestor 绑定源的好处。

编辑

按照评论中的建议,我尝试将绑定更改为如下所示:

Visibility="{Binding Path=DataContext.Settings.ShowInactivePayees, ElementName=PageName,
             Converter={StaticResource AccountStatusToVisibilityConverter}}">

但是当我在 AccountStatusToVisibilityConverter 中设置断点时,转换器永远不会到达。

如何在 GridView 中分配 ItemTemplate?如果您不修改模板的内容而是更改整个模板会怎样?就像这个伪 xaml...

<Page.Resources>
    <DataTemplate x:Key="ActiveTemplate"></DataTemplate>
    <DataTemplate x:Key="InactiveTemplate"></DataTemplate>
    <local:IsActiveToItemTemplateConverter x:Key="IsActiveToTemplate"
        ActiveTemplate="{StaticResource ActiveTemplate}"
        InactiveTemplate="{StaticResource InactiveTemplate}"
        />
</Page.Resources>

<GridView
    ItemsSoucre="{Binding Payees}"
    ItemTemplate="{Binding Settings.ShowInactive, Converter={StaticResource IsActiveToTemplate}}"
    IsItemClickEnabled="{Binding Settings.ShowInactive, Converter={StaticResource BooleanNot}}"
    />