开发者

Accessing mediaelement in listbox wp7

开发者 https://www.devze.com 2023-03-25 04:42 出处:网络
I have a MediaElement inside ListBox.How I can get access to \"audiop_Copy\" by buttons \"play/pause\"?

I have a MediaElement inside ListBox.How I can get access to "audiop_Copy" by buttons "play/pause"?

<local:TypeTemplateSelector.WithAudio>
  <DataTemplate>
    <Grid Margin="0,5">
      <Grid.ColumnDefinitions>
        <ColumnDef开发者_运维百科inition Width="Auto" />
        <ColumnDefinition Width="*"/>
      </Grid.ColumnDefinitions>
      <StackPanel Grid.Column="1">
        <TextBlock ... />
        <StackPanel Height="50" Orientation="Horizontal" Margin="5,0,4,0" MinHeight="50">
          </TextBlock>
          <Button Click="PlayMedia" Content="Play" />
          <Button Click="PauseMedia" Content="Pause" />
        </StackPanel>
        <MediaElement Name="audiop_Copy" Source="{Binding audioUri}" Stretch="None" HorizontalAlignment="Left" AutoPlay="False"/>
      </StackPanel>
    </Grid>
  </DataTemplate>
</local:TypeTemplateSelector.WithAudio>


2 ways to do it from the spot (possibly there are more). You will need a pointer to your Button that was clicked anyway:

  1. [difficult, inflexible, fragile] In button Click event handler use VisualTreeHelper class to navigate Visual Tree and find the element. Use sender as starting point
  2. [better solution] use Tag property and binding.

    <Button Click="PauseMedia" Content="Pause" Tag={Binding ElementName=audiop_Copy} />
    

And in handler something like that:

private void PauseMedia(object sender, RoutedEventArgs e)
{
    var me = ((FrameworkElement) sender).Tag as MediaElement;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消