访问控制XAML中的DataTemplate从代码隐藏? [英] Access XAML Control In DataTemplate From CodeBehind?

查看:129
本文介绍了访问控制XAML中的DataTemplate从代码隐藏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我不能在代码隐藏访问控制,我相信那是因为它是在DataTempalte定义。



总体控制是一个幻灯片放映转盘。每个幻灯片可以是图片或MediaElement的(视频),其内容在一个的ItemSource结合被定义。传送带是一个计时器,切换从一个滑动到下一个。每次滑动改变我触发事件,以这种效果。



当我打的幻灯片与视频,我想停止放映定时器(做到这一点),并启动视频,这是我碰到的一个问题。我无法从我的代码隐藏访问的MediaPlayer 元素名称。我在这一点假设,是因为它是一个DataTemplate。



这是假设是正确的?如果是这样,我怎么可以从代码隐藏,或(更重要的一点)访问这个控制有它开始播放时,幻灯片出现?



 <按Ctrl:AutoScrollCarousel ...> 
<按Ctrl:AutoScrollCarousel.ItemsPanel>
< ItemsPanelTemplate>
< StackPanel的方向=横向/>
< / ItemsPanelTemplate>
< / CTRL:AutoScrollCarousel.ItemsPanel>
<按Ctrl:AutoScrollCarousel.ItemTemplate>
<&DataTemplate的GT;
< BORDER X:名称=边框VerticalAlignment =中心
WIDTH ={结合ActualWidth的,的RelativeSource = {的RelativeSource AncestorType = {X:类型用户控件},模式= FindAncestor}}> ;
<网格背景=白>

<图像来源={结合ContentImage}Grid.Row =1Grid.Column =1拉伸=UniformToFill
的Horizo​​ntalAlignment =中心
能见度={结合ContentImage,转换器= {StaticResource的VisibilityConverter}}/>

< MediaElement的名称=MediaPlayer的来源={结合ContentVideo}Grid.Row =1Grid.Column =1拉伸=UniformToFillLoadedBehavior =播放
能见度={结合ContentVideo,转换器= {StaticResource的VisibilityConverter}}MediaEnded =MediaPlayer_MediaEnded/>

< TextBlock的Grid.Row =0Grid.Column =1文本={结合标题}前景=黑
的FontFamily =的Segoe UI粗细= 光的Horizo​​ntalAlignment =左字号=75保证金=0VerticalAlignment =中心/>

< TextBlock的Grid.Row =2Grid.Column =1文本={结合ContentHeadline}前景=黑
的FontFamily =的Segoe UI粗细= 光的Horizo​​ntalAlignment =左字号=50VerticalAlignment =中心
TextWrapping =自动换行>
< / TextBlock的>
< /网格和GT;
< /边框>
< / DataTemplate中>
< / CTRL:AutoScrollCarousel.ItemTemplate>
< / CTRL:AutoScrollCarousel>


解决方案

我通常会建议不要从代码中触摸UI元素。 ..但MediaElement的是一种特殊情况......也许你应该换整个模板的用户控件(也许有一些自定义DepProps)内,这将使你在整件事更好的控制。



编辑:另一种方法是创建一个带有一些属性(如IsPlaying模块)的行为,并从那里操纵的MediaElement。然后,你可以在DataTemplate中的XAML中使用此行为,而无需后面的代码或用户控件。


I have a control that I am unable to access in the codebehind, and I believe it is because it is defined in a DataTempalte.

The overall control is a slide show carousel. Each slide can be an Image or a MediaElement (video), the contents of which are defined in an ItemSource binding. The carousel is on a timer to switch from one slide to the next. Each time the slide changes I fire an event to that effect.

When I hit a slide with a video I'd like to stop the slide timer (done that) and start the video, which is where I've run into a problem. I can not access the MediaPlayer element Name from my codebehind. My assumption at this point is because it is a DataTemplate.

Is this assumption correct? If so, how can I get access to this control from the codebehind, or (more to the point) have it start playing when the slide comes up?

<ctrl:AutoScrollCarousel ...>
    <ctrl:AutoScrollCarousel.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal" />
        </ItemsPanelTemplate>
    </ctrl:AutoScrollCarousel.ItemsPanel>
    <ctrl:AutoScrollCarousel.ItemTemplate>
        <DataTemplate>
            <Border x:Name="Border" VerticalAlignment="Center"
                    Width="{Binding ActualWidth, RelativeSource={RelativeSource AncestorType={x:Type UserControl},Mode=FindAncestor}}">
                <Grid Background="White">
                    ...
                    <Image Source="{Binding ContentImage}" Grid.Row="1" Grid.Column="1" Stretch="UniformToFill"
                            HorizontalAlignment="Center"
                            Visibility="{Binding ContentImage, Converter={StaticResource VisibilityConverter}}" />

                    <MediaElement Name="MediaPlayer" Source="{Binding ContentVideo}" Grid.Row="1" Grid.Column="1" Stretch="UniformToFill" LoadedBehavior="Play"
                                    Visibility="{Binding ContentVideo, Converter={StaticResource VisibilityConverter}}" MediaEnded="MediaPlayer_MediaEnded" />

                    <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Title}" Foreground="Black"
                                FontFamily="Segoe UI" FontWeight="Light" HorizontalAlignment="Left" FontSize="75" Margin="0" VerticalAlignment="Center" />

                    <TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding ContentHeadline}" Foreground="Black"
                                FontFamily="Segoe UI" FontWeight="Light" HorizontalAlignment="Left" FontSize="50" VerticalAlignment="Center"
                                TextWrapping="Wrap">
                    </TextBlock>
                </Grid>
            </Border>
        </DataTemplate>
    </ctrl:AutoScrollCarousel.ItemTemplate>
</ctrl:AutoScrollCarousel>

解决方案

I would normally recommend not to touch UIElements from code... but the MediaElement is a special case... maybe you should wrap the whole template inside a usercontrol (maybe with some custom DepProps) and that will give you better control over the whole thing.

Edit: Another approach would be to create a Behavior with a couple of properties (such as IsPlaying) and manipulate the mediaelement from there. Then you could use this behavior in the XAML of the DataTemplate, with no need for code behind or usercontrols.

这篇关于访问控制XAML中的DataTemplate从代码隐藏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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