2013-10-15 8 views
5

こんにちは、私はDataTemplateの中にMediaElementを持っていますが、コードの背後からアクセスできません。以下コードテンプレートからデータテンプレート内のコントロールにアクセスするには?

私が投稿していますXAMLコード:背後

<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> 

コード:

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;       
} 

があなたの誰もがそれを行うことができる方法を教えていただけますか?

+0

可能な複製http://stackoverflow.com/questions/8126700/how-do-i-access-an-element-of-a-control-template-from-within-code-behind –

+0

[Access a ContentPresenterのDataTemplateからのコードの中にTextBoxという名前が付けられています(http://stackoverflow.com/questions/1415771/access-a-named-textbox-in-the-code-behind-from-a-contentpresenters-datatemplate)..答えはこの質問をご覧ください。また、[XBrowderのDataTemplateでXAMLコントロールにアクセスする](http://stackoverflow.com/questions/13166726/access-xaml-control-in-datatemplate-from-codebehind)ポストでは、別の回答を見つけることもできます。 – Sheridan

+0

MVVMの使用はどうですか? MediaElementのソースを取得して設定する – JSJ

答えて

10

ですListBoxItem秒の1からContentPresenter

ContentPresenter contentPresenter = FindVisualChild<ContentPresenter>(yourListBoxItem); 

その後ContentPresenterからDataTemplateを得る:

DataTemplate yourDataTemplate = contentPresenter.ContentTemplate; 

その後DataTemplateからMediaElementを得る:

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

詳細については、MSDNにFrameworkTemplate.FindName Methodのページをご覧ください。

+0

+1この質問には意味がある回答だけです。 – Nitin

+0

ありがとうございます。残念ながら、私はこの回答に対して悪意のある、または報復的な投票を行ったようです。いくつかの人々は未熟です。 – Sheridan

+0

それらを無視..あなたが揺れて! – Nitin

0

あなた取得するには、最初の... FrameworkTemplate.FindNameの方法を使用してコントロールにアクセスできるようにする必要がありますがListBoxItemであるあなたのイベントハンドラに送信者を持っている、とのMediaElementはListBoxItem.Content

var mediaElement = ((ListBoxItem)sender).Content as MediaElement; 
if (mediaElement != null) ... 
+0

-1私はあなたが間違っていると信じています。 ['FrameworkTemplate.FindName'](http://msdn.microsoft.com/en-us/library/system.windows.frameworktemplate.findname.aspx)メソッドを使用できます。 – Sheridan

+0

さて、大丈夫、あなたはvidList.DoSomething()のような変数名ではできないということです。そしてあなたのソリューションを使用するには、何とかFrameworkTemplateにアクセスする必要があります。そして私は簡単な解決策を提供しました。 –

+0

あなたがあなたの答えを不正確なもので編集した場合、この投票結果を削除してもよろしいですか?あなたの名前ではできません*文が間違っています。 – Sheridan

関連する問題