Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从代码隐藏访问数据模板中的控件?_C#_Wpf_Xaml - Fatal编程技术网

C# 如何从代码隐藏访问数据模板中的控件?

C# 如何从代码隐藏访问数据模板中的控件?,c#,wpf,xaml,C#,Wpf,Xaml,我在DataTemplate中有一个MediaElement,但我无法从后面的代码访问它 我在下面发布XAML代码: <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="605*"/> <ColumnDefinition Width="151*"/> </Grid.ColumnDefinitions> <Group

我在
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>
</Grid>

有谁能告诉我怎么做吗?

事件处理程序中有sender,它是ListBoxItem,MediaElement是ListBoxItem.Content

var mediaElement = ((ListBoxItem)sender).Content as MediaElement;
if (mediaElement != null) ...
您应该能够使用
FrameworkTemplate.FindName
方法访问控件。。。首先,从
ListBoxItem
s中的一个获取
ContentPresenter

ContentPresenter contentPresenter = FindVisualChild<ContentPresenter>(yourListBoxItem);
然后从
DataTemplate
中获取
MediaElement

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

有关更多信息,请参阅MSDN上的页面。

可能重复的。。。请看这个问题的答案。你也可以在帖子中找到不同的答案。使用MVVM怎么样?要获取并设置MediaElement-1的源代码,我认为您是错误的。你可以使用这个方法。好吧,我的意思是,你不能用像vidList.DoSomething()这样的变量名。要使用您的解决方案,您需要以某种方式使用FrameworkTemplate。我提供了一个简单可行的解决方案。如果你用一些不太准确的东西编辑你的答案,我很乐意删除这个否决票。。。你的“你不能按姓名做”陈述不正确。+1是这个问题唯一有意义的答案。。谢谢@nit。。。不幸的是,似乎我对这个答案投了恶意或报复性的否决票。。。根本没有其他理由否决它。。。。有些人太不成熟了,别理他们。。你太棒了!!!你太好了@nit.)不管怎样,我不介意失去2点声誉。。。我有很多多余的。令人遗憾的是,有这样的人以这种痛苦和扭曲的方式使用StackOverflow。我给那个用户的建议是:长大点!值得指出的是,FindVisualChild()方法在WPF或.NET中本机不可用。您将需要手动添加它,请参见以下SO答案以获取示例:
DataTemplate yourDataTemplate = contentPresenter.ContentTemplate;
MediaElement yourMediaElement = yourDataTemplate.FindName("vidList", contentPresenter) 
as MediaElement;
if (yourMediaElement != null)
{
    // Do something with yourMediaElement here
}