我正在从事WPF MVVM项目。我有一个Listview,它在Grid中有一个Image和一个MediaElement。我的目标是在鼠标悬停在图像上时播放视频。 因为控件位于ItemTemplate中,所以我无法从后面的代码中访问它们:
private void photoC_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
//mediaplayerC doesnt exist
}
我的XAML:
<ListView Name="photosListC" Background="Beige" Padding="15"
ItemsSource="{Binding PhotosList}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" >
<ListView.ItemTemplate>
<DataTemplate>
<Grid Name="mainGrid" MouseEnter="mainGrid_MouseEnter">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="200"/>
</Grid.RowDefinitions>
<MediaElement Name="mediaplayerC" Source="{Binding VidSource}"
Stretch="UniformToFill" HorizontalAlignment="Center" Panel.ZIndex="2" LoadedBehavior="Manual"
MouseEnter="mediaplayerC_MouseEnter" >
</MediaElement>
<Image Name="photoC" Stretch="UniformToFill" HorizontalAlignment="Center" Panel.ZIndex="3" MouseEnter="photoC_MouseEnter">
<Image.Source>
<BitmapImage UriSource="{Binding ImSource}" />
</Image.Source>
</Image>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
解决此问题的最佳方法是什么?