WinRT的XAML数据绑定:一个ItemTemplate内绑定时如何绑定到物业给母公司数据上下文?

2019-10-22 12:52发布

我有一个GridView为此我编程方式设置数据上下文视图模型实例。 GridView的ItemsSource绑定到一个观察集合(PagesToRead),这是在视图模型的属性。

GridView.ItemTemplate,结合顶在的ItemsSource观察集合,但我想给的StackPanel的背景元素绑定到视图模型不同的属性。

我正在寻找的魔术<Background="{Binding Path=BackgroundColor, Source=???}">将退出当前的ItemsSource和绑定到视图模型backgroundColor属性。

这里的消隐XAML:

<Grid>
  <GridView x:Name="MainGrid" CanReorderItems="True" CanDragItems="True" 
    ItemsSource="{Binding Path=PagesToRead}"
    <GridView.ItemTemplate>
      <DataTemplate >
          <StackPanel>
            <Background="{Binding Path=BackgroundColor, Source=???}">
            <TextBlock Text="{Binding Path=Title}" 
          </StackPanel>
      </DataTemplate>
    </GridView.ItemTemplate>
  </GridView>
</Grid>

Answer 1:

我通过其他途径(感谢卡尔·埃里克森)得到了一个答案。 你要做的就是:

<StackPanel Background="{Binding Path=DataContext.TileBackgroundColor,
                         ElementName=MainGrid">


文章来源: WinRT XAML Databinding: How to bind to a property to the parent's data context when binding within an ItemTemplate?