Reputation: 2739
I am facing problem and would be glad if you could help . So,I am binding "Listview" to "Feeds" object which is a FeedViewModel object.
But at some point ,I want to show some data which isn't present in the FeedViewModel object but is present in the MainViewModel object. In my case,for ex-i want to display the URL for every feed but the URL isn't being extracted from the Xpath extraction of source but being passed from the MainViewModel object which I have shown in code by MainViewModel.Url.
But in my XAML,all the children of the Listview look only in the "Feeds" object which is creating a problem.
<ListView Grid.Row="3" Margin="5" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Auto" HorizontalContentAlignment="Stretch" ItemsSource="{Binding Feeds}">
<ListView.ItemTemplate>
<DataTemplate>
--------------some code --------------
<Label Margin="4">Critic:</Label>
<Label Grid.Column="1" Content="{Binding Creator}" Margin="4" />
<Label Grid.Row="1" Margin="4">Title:</Label>
<Label Grid.Column="1" Grid.Row="1" Content="{Binding Title}" Margin="4" FontWeight="Bold" />
<Label Grid.Row="2" Margin="4">Location:</Label>
<Label Grid.Column="1" Grid.Row="2" Content="{Binding **MainViewModel.Url**}" Margin="4" />
<Label Grid.Row="3" Margin="4">Date:</Label>
<Label Grid.Column="1" Grid.Row="3" Content="{Binding Date}" Margin="4" />
<Label Grid.Row="4" Margin="4">Rating:</Label>
<Label Grid.Column="1" Grid.Row="4" Content="{Binding Rating}" Margin="4" />
<Label Grid.Row="5" Margin="4" HorizontalAlignment="Stretch">Description:</Label>
<TextBlock Grid.Column="1" Grid.Row="5" TextWrapping="Wrap" ScrollViewer.VerticalScrollBarVisibility="Auto" Text="{Binding Description}" Padding="4" Margin="4" />
</Grid>
</Border>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Can someone suggest what can be done?
Gaurav
Upvotes: 0
Views: 645
Reputation: 184777
The DataContext
is changed to the templated item, to get to the main viewmodel you can target a parent's DataContext
using RelativeSource
, e.g.
<Label Grid.Column="1" Grid.Row="2"
DataContext="{Binding DataContext, RelativeSource={RelativeSource AncestorType=ListView}}"
Content="{Binding Url}" Margin="4" />
Upvotes: 1