【问题标题】:WinRT XAML Databinding: How to bind to a property to the parent's data context when binding within an ItemTemplate?WinRT XAML 数据绑定:在 ItemTemplate 中绑定时如何将属性绑定到父数据上下文?
【发布时间】:2015-06-11 14:06:29
【问题描述】:

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

GridView.ItemTemplate 中,绑定与 ItemsSource 中的可观察集合相反,但我想将 StackPanel 的 Background 元素绑定到视图模型上的不同属性。

我正在寻找能够转义当前 ItemsSource 并绑定到视图模型上的 BackgroundColor 属性的魔法 <Background="{Binding Path=BackgroundColor, Source=???}">

这是省略的 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>

【问题讨论】:

    标签: c# xaml windows-runtime


    【解决方案1】:

    我通过其他途径得到了答案(感谢 Karl Erickson)。你要做的是:

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

    【讨论】:

    • 感谢分享,您可以将此标记为答案!
    • 这对我不起作用,因为我的 DataTemplate 位于单独的 xaml 文件中,并且在多个父级中使用。
    猜你喜欢
    • 1970-01-01
    • 2016-03-15
    • 1970-01-01
    • 2013-01-20
    • 2014-08-11
    • 1970-01-01
    • 1970-01-01
    • 2013-10-09
    • 2011-03-11
    相关资源
    最近更新 更多