10

I have a MediaElement within the DataTemplate but I am unable to access it from the code behind.

I am posting XAML code below:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="605*"/>
        <ColumnDefinition Width="151*"/>
    </Grid.ColumnDefinitions>
    <GroupBox Header="My Videos" Height="177" VerticalAlignment="Top" Margin="5,320,5,0" Grid.ColumnSpan="2">
        <ListBox x:Name="VideoList" ItemsSource="{Binding Videos }" Width="auto" Height=" auto" Margin="0,0,0,0" Grid.ColumnSpan="2" >
            <DataTemplate x:Name="DTVideos">
                <ListBoxItem Name="lbivid1" BorderThickness="2"  Width="240" Selected="lbivid_Selected" >
                    <MediaElement Name="vidList" Height="150" Width="150" Source="{Binding SourceUri}" Position="00:00:05" LoadedBehavior="Pause" ScrubbingEnabled="True"/>
                </ListBoxItem>
            </DataTemplate>
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal" Margin="0,0,0,0"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ListBox>
    </GroupBox>     
    <GroupBox Header="Preview" Height="320" Width="400" VerticalAlignment="Top" DockPanel.Dock="Left">
        <MediaElement x:Name="videoPreview" HorizontalAlignment="Left" Height="300" VerticalAlignment="Top" Width="388"/>
    </GroupBox>
</Grid>

Code behind:

 private void lbivid_Selected(object sender, RoutedEventArgs e)
 {   
    imagePreview.Visibility = Visibility.Hidden;   
    string urlStr = (VidList.Source).ToString();          
    Uri temp = new Uri(UrlStr);
    videoPreview.Source = temp;                         
 }   

Can anyone of you please tell me how can it be done?

Eliahu Aaron
  • 4,103
  • 5
  • 27
  • 37
Badrul
  • 1,582
  • 4
  • 16
  • 27
  • Possible duplicate http://stackoverflow.com/questions/8126700/how-do-i-access-an-element-of-a-control-template-from-within-code-behind – Omri Btian Oct 15 '13 at 11:23
  • Duplicate of [Access a named TextBox in the code behind from a ContentPresenter's DataTemplate](http://stackoverflow.com/questions/1415771/access-a-named-textbox-in-the-code-behind-from-a-contentpresenters-datatemplate)... please see this question for the answer. You can also find a different answer in the [Access XAML Control In DataTemplate From CodeBehind?](http://stackoverflow.com/questions/13166726/access-xaml-control-in-datatemplate-from-codebehind) post. – Sheridan Oct 15 '13 at 11:57
  • How about using MVVM ? to get and set the Source of the MediaElement – JSJ Oct 15 '13 at 13:29

2 Answers2

26

You should be able to access your control using the FrameworkTemplate.FindName method... first, get the ContentPresenter from one of the ListBoxItems:

ContentPresenter contentPresenter = FindVisualChild<ContentPresenter>(yourListBoxItem);

Then get the DataTemplate from the ContentPresenter:

DataTemplate yourDataTemplate = contentPresenter.ContentTemplate;

Then get the MediaElement from the DataTemplate:

MediaElement yourMediaElement = yourDataTemplate.FindName("vidList", contentPresenter) 
as MediaElement;
if (yourMediaElement != null)
{
    // Do something with yourMediaElement here
}

Please see the FrameworkTemplate.FindName Method page on MSDN for more information.

Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • 1
    +1 for only answer that makes sense to this question.. – Nitin Oct 15 '13 at 12:27
  • Thanks @nit... unfortunately, it seems as though I've been given a spiteful or retaliatory down vote on this answer... there's simply no other reason to down vote it.... some people are so immature. – Sheridan Oct 15 '13 at 12:50
  • ignore them.. you rock!!! – Nitin Oct 15 '13 at 12:53
  • You're too kind @nit. :) Either way, I don't mind losing the 2 reputation points... I have plenty to spare. It's just a shame that there are people like this using StackOverflow in this bitter and twisted way. My advice to that user: Grow up! – Sheridan Oct 15 '13 at 13:00
  • 4
    It is worth pointing out that the FindVisualChild<>() method is not _natively_ available in WPF or .NET. You will need to manually add it, see the following SO answer for an example: http://stackoverflow.com/a/25229554/2737435 – XtraSimplicity Jan 11 '16 at 02:22
  • @XtraSimplicity, thanks for adding that comment and link. Good point. – Sheridan Jan 13 '16 at 14:40
  • Not a problem; hopefully it helps someone who hasn't seen that function before. :) – XtraSimplicity Jan 13 '16 at 21:17
-2

You have sender in your event handler, which is ListBoxItem, and MediaElement is ListBoxItem.Content

var mediaElement = ((ListBoxItem)sender).Content as MediaElement;
if (mediaElement != null) ...
Pavel Tupitsyn
  • 8,393
  • 3
  • 22
  • 44
  • -1 I believe that you are incorrect. You can use the [`FrameworkTemplate.FindName`](http://msdn.microsoft.com/en-us/library/system.windows.frameworktemplate.findname.aspx) method. – Sheridan Oct 15 '13 at 12:02
  • Well, okay, I mean you can't do it by a variable name like vidList.DoSomething(). And to use your solution you will need to get to FrameworkTemplate somehow. And I provided a simple working solution. – Pavel Tupitsyn Oct 15 '13 at 12:08
  • I'm happy to remove this down vote if you edit your answer with something less inaccurate... your *You can't do it by name* statement is incorrect. – Sheridan Oct 15 '13 at 12:14