Terry
Terry

Reputation: 842

WinRT XAML Find control inside FlipView Item template

So I have a flip view to display an article and in this flip view I have a Media Player from the Microsoft Player Framework. Now I want to be able to access this media player from code.

However, every single example I have found has used FindName on the DataTemplate which WinRT doesn't seen to have.

I have tried

var flipViewItem = this.flipView.ItemContainerGenerator.ContainerFromIndex(flipView.SelectedIndex);

and then using the VisualTreeHelper to find the Media Player but flipViewItem is always null.

I tried data binding the video player - but this causes all videos in the flip view items to play simultaneously.

Can someone please help?

EDIT

So this is my xaml that I am using:

<FlipView
        x:Name="flipView"
        AutomationProperties.AutomationId="ItemsFlipView"
        AutomationProperties.Name="Item Details"
        TabIndex="1"
        Margin="0,127,0,10"
        ItemsSource="{Binding Source={StaticResource itemsViewSource}}" Grid.RowSpan="2">

        <FlipView.ItemTemplate>
            <DataTemplate>
                <UserControl Loaded="StartLayoutUpdates" Unloaded="StopLayoutUpdates">
                    <ScrollViewer x:Name="scrollViewer" Style="{StaticResource HorizontalScrollViewerStyle}" Grid.Row="1">

                        <common:RichTextColumns x:Name="richTextColumns" Margin="117,0,117,47">
                            <RichTextBlock x:Name="richTextBlock" Width="560" Style="{StaticResource ItemRichTextStyle}">
                                <Paragraph>
                                    <Run FontSize="26.667" FontWeight="Light" Text="{Binding Title}"/>
                                </Paragraph>
                                <Paragraph LineStackingStrategy="MaxHeight">
                                    <InlineUIContainer>
                                        <Grid Width="560" Height="340" Margin="0,20,0,10">
                                            <Grid.Background>
                                                <ImageBrush ImageSource="{Binding Image}" Stretch="UniformToFill" />
                                            </Grid.Background>
                                            <Image x:Name="OverlayImage" Visibility="{Binding ShowVideo, Converter={StaticResource booleanToVisibilityConverter}}" Source="Assets/play-icon.png"/>
                                            <mmppf:MediaPlayer x:Name="ArticleVideo" Width="560" Height="340" />
                                        </Grid>
                                    </InlineUIContainer>

I have tried binding to the media player:

<mmppf:MediaPlayer x:Name="ArticleVideo" Source="{Binding VideoPath}" Width="560" Height="340" PosterSource="{Binding Image}" AutoPlay="False" />

However, when I do that and set the videos to autoplay - they all autoplay. If I manually press play on the control, I receive a null reference exception at System.ComponentModel.PropertyChangedEventHandler.Invoke. The media player works fine outside the template.

What is confusing, is that I know what I want to is possible. The SkyNews app for windows 8 does exactly what I want to do (add a video to a flip view item template). I had a look in c:\ProgramFiles\WindowsApps so see what their xaml looked like at it's the almost the same as mine - with no data binding on the media player. The only difference is that they have x:ConnectionId on the usercontrol and the grid containing the video player in the template. I don't know what x:ConnectionId does - but is it possible they are someone using that to access the controls from their code?

Upvotes: 4

Views: 8527

Answers (2)

Jerry Nixon
Jerry Nixon

Reputation: 31813

Sure. If you want to access a named control in a template - look here http://blog.jerrynixon.com/2012/09/how-to-access-named-control-inside-xaml.html

Upvotes: 0

Jeff Brand
Jeff Brand

Reputation: 5633

Check out http://winrtxamltoolkit.codeplex.com/downloads/get/467926 and look at the VisualTreeHelperExtensions.

There is not direct method to find a the element in the visual tree. You need to use the VisualTreeHelper.GetChild(flipView, i) method. Not hard to implement, but the Toolkit has a single extension method class file you can add directly to your project. It provides a way to get descendants or ancestors by type. I think it will fit the bill for what you are looking for.

Upvotes: 2

Related Questions