WinRT What could be instead of AncestorType?

I want to get the number of ListView items. But it uses a template, so I need to use AncestorType, I have code that works fine in WPF, but not in Windows Store Apps 8, because AncestorType is not there, so what can I do instead? How can I make this code work in winRT?

Here is my code:

<ListView ItemsSource="{Binding Users}"> <ListView.Style> <Style TargetType="ListView"> <Style.Setters> <Setter Property="Template"> <Setter.Value> <ControlTemplate> <Border BorderThickness="1" BorderBrush="LightGray"> <StackPanel> <ScrollViewer> <ItemsPresenter /> </ScrollViewer> <TextBlock Margin="0,4" FontWeight="Bold"> <Run Text="Count: "/> <Run Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListView}}, Path=Items.Count, Mode=OneWay}"/> </TextBlock> </StackPanel> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style.Setters> </Style> </ListView.Style> <ListView.ItemTemplate> <DataTemplate> <ListViewItem IsHitTestVisible="False"> <StackPanel> <facebookControls:ProfilePicture Height="74" Width="74" ProfileId="{Binding FacebookId}" /> <TextBlock Text="{Binding UserName}" FontSize="18" HorizontalAlignment="Center" /> </StackPanel> </ListViewItem> </DataTemplate> </ListView.ItemTemplate> </ListView> 
+7
winrt-xaml xaml
source share
4 answers

As you are in the ControlTemplate , you need to somehow break free and access the underlying DataContext . It depends on how you defined the object to which you are attached. For example, if you defined a ViewModel in resources, you can access it in the same way as the Users property:

 <UserControl ... > <UserControl.Resources> <vm:MyViewModel x:Key="ViewModel" /> </UserControl.Resources> ... <Grid DataContext="{Binding Source={StaticResource ViewModel}}"> <ListView ItemsSource="{Binding Users}"> ... <ControlTemplate> ... <Run Text="{Binding Source={StaticResource ViewModel}, Path=Users.Count}"/> 

This is one way to handle this.

+1
source share

The answer posted by Ed Chapel has one big flaw:

 <vm:MyViewModel x:Key="ViewModel" /> 

causes MyViewModel be built one more time. In normal scenarios, this is undesirable behavior.

By the way, there is an ideal trick to bind to the parent DataContext without restoring the view model.

Assuming MyViewModel has an "ICommand" named TestCommand and is the current DataContext page that contains your UserControl , sets the x:Name page and simply binds the DataContext page to the Tag property of the UserControl using the ElementName binding:

 <Page... x:Name="rootPage"> <Grid> <controls:MyUserControl Tag={Binding DataContext, ElementName='rootPage'} ... /> </Grid> ... </Page> 

And then in the XAML of your UserControl set x:Name to UserControl and bind your property to the property in Tag :

 <UserControl ... x:Name="rootControl"> <Grid> <Button Command={Binding Tag.TestCommand, ElementName='rootControl'} ... /> </Grid> </UserControl> 

This may not be the cleanest trick that works, because the Tag property is a dependency property of type object , and you can associate anything with it.

+7
source share

The simplest solution is to name any root container that has your view model in Context, and simply set the DataContext of your control / container inside ContentControl, specifying the name of the element, and you will get the same context.

Example (I named my page root grid as "rootGrid" and set the context using DataContext = "{Binding ElementName = rootGrid, Path = DataContext}")

 <ContentControl Name="contentControl" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" > <Grid Height="600" Width="600" DataContext="{Binding ElementName=rootGrid, Path=DataContext}" > <Image Height="500" Width="500" Source="{Binding photoUrl}" VerticalAlignment="Center" HorizontalAlignment="Stretch"/> </Grid> </ContentControl> 
+5
source share

I got it like in UWP

 <GridView x:Name="abc" ItemsSource="{Binding Path=DataContext.Companies,RelativeSource={RelativeSource Mode=TemplatedParent}}"></GridView> 
0
source share

All Articles