无法访问listboxItem的DataTemplate可视元素 [英] Cannot access DataTemplate visual element of listboxItem

查看:68
本文介绍了无法访问listboxItem的DataTemplate可视元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的

我有一个列表框,其定义如下:

I have a list box which is define as below :

<ListBox  x:Name="_vdoItems" Grid.Row="1"  ItemsSource="{Binding ProductVideos, UpdateSourceTrigger=PropertyChanged}" SelectionMode="Single"
                          Margin="5,0,5,0" IsSynchronizedWithCurrentItem="True"
                                                    Style="{StaticResource ListBoxStyle1}"     
                          ItemTemplate="{StaticResource DefaultImageTemplate}"
                          ItemContainerStyle="{StaticResource SelectedItemStyle}"
                               SelectionChanged="_vdoItems_SelectionChanged"
                          >
           
                <ListBox.ItemsPanel>
                    <ItemsPanelTemplate >
                        <UniformGrid Rows="1" Background="Transparent"/>
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>

            </ListBox>

然后我有一个数据模板定义如下:

Then I have a data template define as follow:

<DataTemplate x:Key="DefaultImageTemplate">
                <Grid >
                    <MediaElement x:Name="_video"  Source="{Binding FileUrl,UpdateSourceTrigger=PropertyChanged}" Width="200" Height="180" Margin="5" Stretch="UniformToFill"  Volume="0" LoadedBehavior="Manual" />
                </Grid>
            </DataTemplate>

对于我的每个listox项目,我都需要获取MediaElement对象才能对其进行操作.为此,我像波纹管一样

For each of my listox Item I need to fetch the MediaElement object in order to operate on it. TO do that I do as bellow

foreach (var item in _vdoItems.Items)
            {
                ListBoxItem li = _vdoItems.ItemContainerGenerator.ContainerFromItem(item) as ListBoxItem;
                
                ContentPresenter cp = FindVisualChild<ContentPresenter>(li);
                DataTemplate dt = li.ContentTemplate;
               MediaElement media = dt.FindName("_video", cp) as MediaElement;
               media.Play();
            }

这里发生的是无法检索ContentPresenter并返回null.原因是基于给定的LIstBoxItem作为FindVsualChild的参数,它根本找不到任何子项.

What is happening here is that the ContentPresenter cannot be retrieve and return null. The reason is that based on the given LIstBoxItem as parameter to FindVsualChild, it cannot find any child at all.

请注意,每个列表框项目的Content属性的类型为VideoViewModel

PLease note that the Content property of each listbox item are of type VideoViewModel

要寻求帮助

致谢

推荐答案

我在您的DataTemplate中看不到任何ContentPresenter吗?您可以直接搜索MediaElement的可视树:

I cannot see any ContentPresenter in your DataTemplate? You could search the visual tree for the MediaElement directly:

foreach (var item in _vdoItems.Items)
            {
                ListBoxItem li = _vdoItems.ItemContainerGenerator.ContainerFromItem(item) as ListBoxItem;
                MediaElement media = FindVisualChild<MediaElement>(li);
                media.Play();
            }

此外,您从哪里调用foreach循环?您需要先确保已完全加载视觉树,然后再尝试对其进行迭代.例如,您不应在窗口的构造函数中执行此操作,而应在窗口 已加载:

Also, from where are you calling above foreach loop? You need to make sure that the visual tree has been completely loaded before you try to iterate through it. For example, you should not do this in the constructor of the window but when the window has been loaded:

public MainWindow()
        {
            InitializeComponent();

            ...
            this.Loaded += MainWindow_Loaded;


        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            foreach (var item in _vdoItems.Items)
            {
                ...
            }
        }



这篇关于无法访问listboxItem的DataTemplate可视元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆