Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to access a control within Data Template from code behind? [closed]

Tags:

c#

wpf

xaml

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?

like image 626
Badrul Avatar asked Oct 15 '13 11:10

Badrul


People also ask

How do you access a named control inside a XAML DataTemplate?

So, to get a named element in the DataTemplate you have to first get the generated item, and then search inside that generated item for the element you want. Remember, the Logical Tree in XAML is how you access things by name. Generated items are not in the Logical Tree.

What is the difference between control template and data template?

A ControlTemplate will generally only contain TemplateBinding expressions, binding back to the properties on the control itself, while a DataTemplate will contain standard Binding expressions, binding to the properties of its DataContext (the business/domain object or view model).


1 Answers

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.

like image 106
Sheridan Avatar answered Sep 20 '22 06:09

Sheridan