Accessing XAML management in a DataTemplate from CodeBehind?

I have a control in which I cannot access the code, and I believe that this is because it is defined in the DataTempalte.

General control - slide show carousel. Each slide can be an image or a media element (video), the content of which is defined in the ItemSource binding. The carousel is on the timer to switch from one slide to another. Every time the slide changes, I fire an event for this.

When I click on a video slide, I would like to stop the slide timer (do it) and start the video in which I encountered a problem. I cannot access the MediaPlayer Name element from my code. My guess at this point is that this is a DataTemplate.

Is this assumption correct? If so, how can I access this control with a code or (moreover) if it starts playing when a slide appears?

 <ctrl:AutoScrollCarousel ...> <ctrl:AutoScrollCarousel.ItemsPanel> <ItemsPanelTemplate> <StackPanel Orientation="Horizontal" /> </ItemsPanelTemplate> </ctrl:AutoScrollCarousel.ItemsPanel> <ctrl:AutoScrollCarousel.ItemTemplate> <DataTemplate> <Border x:Name="Border" VerticalAlignment="Center" Width="{Binding ActualWidth, RelativeSource={RelativeSource AncestorType={x:Type UserControl},Mode=FindAncestor}}"> <Grid Background="White"> ... <Image Source="{Binding ContentImage}" Grid.Row="1" Grid.Column="1" Stretch="UniformToFill" HorizontalAlignment="Center" Visibility="{Binding ContentImage, Converter={StaticResource VisibilityConverter}}" /> <MediaElement Name="MediaPlayer" Source="{Binding ContentVideo}" Grid.Row="1" Grid.Column="1" Stretch="UniformToFill" LoadedBehavior="Play" Visibility="{Binding ContentVideo, Converter={StaticResource VisibilityConverter}}" MediaEnded="MediaPlayer_MediaEnded" /> <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Title}" Foreground="Black" FontFamily="Segoe UI" FontWeight="Light" HorizontalAlignment="Left" FontSize="75" Margin="0" VerticalAlignment="Center" /> <TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding ContentHeadline}" Foreground="Black" FontFamily="Segoe UI" FontWeight="Light" HorizontalAlignment="Left" FontSize="50" VerticalAlignment="Center" TextWrapping="Wrap"> </TextBlock> </Grid> </Border> </DataTemplate> </ctrl:AutoScrollCarousel.ItemTemplate> </ctrl:AutoScrollCarousel> 
+1
source share
2 answers

I usually recommend not touching UIElements from the code ... but MediaElement is a special case ... maybe you should wrap the whole template inside a usercontrol (maybe with some custom DepProps) and this will give you better control over all this.

Edit: Another approach was to create a Behavior with several properties (e.g. IsPlaying) and manipulate the media element from there. You can then use this behavior in the XAML DataTemplate, without the need for code or user control.

+2
source

WPF provides a simple and easy way to access named elements that are generated from DataTemplates. This is explained in the MSDN article How to Find Items Created Using a DataTemplate .

Assuming your AutoScrollCarousel is obtained from ItemsControl, you get a ContentPresenter, which is a container of such an element:

 AutoScrollCarousel carousel = ... object item = ... var contentPresenter = carousel.ItemContainerGenerator.ContainerFromItem(item) as ContentPresenter; 

From ContentPresenter, you get the named element in the DataTemplate using the FindName method:

 var dataTemplate = contentPresenter.ContentTemplate; var mediaPlayer = dataTemplate.FindName("MediaPlayer", contentPresenter) as MediaElement; 
+2
source

Source: https://habr.com/ru/post/988994/


All Articles