Xin chào, tôi có một số MediaElement
trong số DataTemplate
nhưng tôi không thể truy cập nó từ mã phía sau.cách truy cập một điều khiển trong Mẫu Dữ liệu từ mã phía sau?
tôi gửi mã XAML dưới đây:
<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>
Mã đằng sau:
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;
}
ai trong các bạn có thể vui lòng cho tôi biết làm thế nào nó có thể được thực hiện?
Có thể trùng lặp http://stackoverflow.com/questions/8126700/how-do-i-access-an-element-of-a-control-template-from-within-code-behind –
Bản sao của [Truy cập được đặt tên là TextBox trong mã phía sau từ DataTemplate của ContentPresenter] (http://stackoverflow.com/questions/1415771/access-a-named-textbox-in-the-code-behind-from-a-contentpresenters-datatemplate) .. xin vui lòng xem câu hỏi này cho câu trả lời. Bạn cũng có thể tìm thấy một câu trả lời khác trong [Truy cập XAML kiểm soát trong DataTemplate từ CodeBehind?] (Http://stackoverflow.com/questions/13166726/access-xaml-control-in-datatemplate-from-codebehind) bài. – Sheridan
Làm thế nào về việc sử dụng MVVM? để nhận và thiết lập Nguồn của MediaElement – JSJ