繁体   English   中英

WinRT XAML数据绑定:在ItemTemplate中进行绑定时,如何将属性绑定到父级的数据上下文?

[英]WinRT XAML Databinding: How to bind to a property to the parent's data context when binding within an ItemTemplate?

我有一个GridView ,可以通过编程将其数据上下文设置为视图模型实例。 GridView的ItemsSource绑定到一个可观察的集合(PagesToRead),该集合是视图模型的一个属性。

GridView.ItemTemplate内 ,绑定与ItemsSource中的observable集合冲突,但是我想将StackPanel的Background元素绑定到视图模型上的其他属性。

我正在寻找神奇的<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>

我通过另一条途径得到了答案(感谢卡尔·埃里克森)。 您要做的是:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM