RDaniel
RDaniel

Reputation: 49

How do I access a control of a DataTemplate

I have two different DataTemplates in an UserControl Resource. One DataTemplate contains a image control and the other DataTemplate a media element control. The DataType of each DataTemplate represents a ImageViewModel respectively a VideoViewModel. In my user control a have a grid which contains a ContentControl. The content property of the content control is bound to a property which represents the current view model that should be used.

The idea is to change the content of the grid depending on the current view model

<UserControl.Resources>
    <DataTemplate DataType="{x:Type vm:ImageScreensaverViewModel}">
        <Image Source="{Binding Image}" Stretch="Uniform"/>
    </DataTemplate>

    <DataTemplate DataType="{x:Type vm:VideoScreensaverViewModel}">
        <MediaElement x:Name="Player" Source="{Binding Video}" LoadedBehavior="Play" />
    </DataTemplate>
</UserControl.Resources>

<UserControl.CommandBindings>
    <CommandBinding Command="MediaCommands.Pause" Executed="PausePlayer" CanExecute="CanExecute"/>
    <CommandBinding Command="MediaCommands.Play" Executed="PlayPlayer" CanExecute="CanExecute"/>
</UserControl.CommandBindings>

<Grid>
    <ContentControl x:Name="ScreanSaverContent" Content="{Binding CurrentVm}"/>
</Grid>

This works great, but I need to access the MediaElement in code behind so that I can control the media player (Play, Stop, Pause)

I already tried the solution posted on hier without any success. I can access only the selected view model though the content property.

Upvotes: 0

Views: 2351

Answers (1)

Alex Seleznyov
Alex Seleznyov

Reputation: 945

Try this piece of code to reach to a control inside ContentPresenter:

    public static FrameworkElement GetControlByName(DependencyObject parent, string name)
    {
        int count = VisualTreeHelper.GetChildrenCount(parent);
        for (var i = 0; i < count; ++i)
        {
            var child = VisualTreeHelper.GetChild(parent, i) as FrameworkElement;
            if (child != null)
            {
                if (child.Name == name)
                {
                    return child;
                }
                var descendantFromName = GetControlByName(child, name);
                if (descendantFromName != null)
                {
                    return descendantFromName;
                }
            }
        }
        return null;
    }

Upvotes: 2

Related Questions