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>
感謝分享,你可以把它標記爲答案! –
這對我不起作用,因爲我的DataTemplate在一個單獨的xaml文件中,並且在多個父項中使用。 –