绑定到Page的DataContext

时间:2015-07-14 06:21:49

标签: c# xaml windows-runtime winrt-xaml

我发现需要绑定到DataContext的{​​{1}},其设置如下:

Page

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

<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的{​​{1}}设置为DataContext中的各个GridViewItems个对象收集。我的Payee有一个属性,我需要从转换器中的Payees引用,以确定项目的可见性。

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

View

但我真正需要绑定的是Payee <Border Width="250" Height="250" Background="Gray" Opacity="0.85" Visibility="{Binding Path=IsOpen, Converter={StaticResource AccountStatusToVisibilityConverter}}"> 的{​​{1}}属性。有没有办法从子控件中获取该上下文?我使用的是WinRT,因此我没有Settings.ShowInactive绑定源的好处。

修改

正如评论中所建议的那样,我试图将绑定更改为:

Page

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

1 个答案:

答案 0 :(得分:1)

如何在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}}"
    />