wpf 如何从背后的代码访问数据模板中的控件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19379946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:51:53  来源:igfitidea点击:

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

c#wpfxaml

提问by Badrul

I have a MediaElementwithin the DataTemplatebut I am unable to access it from the code behind.

我有一个MediaElementDataTemplate但我无法从后面的代码访问它。

I am posting XAML code below:

我在下面发布 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>
</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?

你们中的任何人都可以告诉我怎么做吗?

回答by Sheridan

You shouldbe able to access your control using the FrameworkTemplate.FindNamemethod... first, get the ContentPresenterfrom one of the ListBoxItems:

应该能够使用该FrameworkTemplate.FindName方法访问您的控件...首先,ContentPresenter从以下其中一个获取ListBoxItem

ContentPresenter contentPresenter = FindVisualChild<ContentPresenter>(yourListBoxItem);

Then get the DataTemplatefrom the ContentPresenter:

然后DataTemplateContentPresenter

DataTemplate yourDataTemplate = contentPresenter.ContentTemplate;

Then get the MediaElementfrom the DataTemplate:

然后MediaElementDataTemplate

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

Please see the FrameworkTemplate.FindNameMethodpage on MSDN for more information.

有关详细信息,请参阅MSDN 上的FrameworkTemplate.FindName方法页面。

回答by Pavel Tupitsyn

You have sender in your event handler, which is ListBoxItem, and MediaElement is ListBoxItem.Content

您的事件处理程序中有发送者,即 ListBoxItem,MediaElement 是 ListBoxItem.Content

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