2015-04-07 41 views
1

我有一個GridView我設置數據上下文以編程方式到視圖模型實例。 GridView的ItemsSource綁定到一個可觀察集合(PagesToRead),它是視圖模型上的一個屬性。WinRT XAML Databinding:在ItemTemplate中綁定時如何將屬性綁定到父級的數據上下文?

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> 

回答

1

我通過其他途徑(感謝卡爾·埃裏克森)的答案。你要做的是:

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

感謝分享,你可以把它標記爲答案! –

+2

這對我不起作用,因爲我的DataTemplate在一個單獨的xaml文件中,並且在多個父項中使用。 –

相關問題