如何访问 FlipView 内 DataTemplate 内的 xaml 控件

How to access xaml Control inside DataTemplate inside FlipView(如何访问 FlipView 内 DataTemplate 内的 xaml 控件)

本文介绍了如何访问 FlipView 内 DataTemplate 内的 xaml 控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想访问 C# 代码中的图像"元素.我知道我不能直接访问它,因为它在数据模板中.我已经尝试过视觉树,但仍然无法在代码中获取图像"控制元素.

I want to access the "image" element in the C# code. I know that i cannot access it directly since it is in datatemplate. I have tried visual trees but still not able to get "image" control element in the code .

<FlipView
            x:Name="flipView"
            AutomationProperties.AutomationId="ItemsFlipView"
            AutomationProperties.Name="Item Details"
            TabIndex="1"
            Grid.RowSpan="2"
            ItemsSource="{Binding Source={StaticResource itemsViewSource}}" SelectionChanged="flipView_SelectionChanged">

            <FlipView.ItemContainerStyle>
                <Style TargetType="FlipViewItem">
                    <Setter Property="Margin" Value="0,137,0,0"/>
                </Style>
            </FlipView.ItemContainerStyle>

            <FlipView.ItemTemplate>
                <DataTemplate>

                    <!--
                        UserControl chosen as the templated item because it supports visual state management
                        Loaded/unloaded events explicitly subscribe to view state updates from the page
                    -->
                    <UserControl Loaded="StartLayoutUpdates" Unloaded="StopLayoutUpdates">
                        <ScrollViewer x:Name="scrollViewer" Style="{StaticResource HorizontalScrollViewerStyle}" Grid.Row="1">

                            <!-- Content is allowed to flow across as many columns as needed -->
                            <common:RichTextColumns x:Name="richTextColumns" Margin="117,0,117,47">
                                <RichTextBlock x:Name="richTextBlock" Width="560" Style="{StaticResource ItemRichTextStyle}" IsTextSelectionEnabled="False">
                                    <Paragraph>
                                        <Run FontSize="26.667" FontWeight="Light" Text="{Binding Title}"/>
                                        <LineBreak/>
                                        <LineBreak/>
                                        <Run FontWeight="Normal" Text="{Binding Subtitle}"/>
                                    </Paragraph>
                                    <Paragraph LineStackingStrategy="MaxHeight">
                                        <InlineUIContainer>
                                            <Image x:Name="image" MaxHeight="480" Margin="0,20,0,10" Stretch="Uniform" Source="{Binding Image}" AutomationProperties.Name="{Binding Title}"/>
                                        </InlineUIContainer>
                                    </Paragraph>                                  
                                </RichTextBlock>
                            </common:RichTextColumns>
                        </ScrollViewer>
                    </UserControl>
                </DataTemplate>
            </FlipView.ItemTemplate>
</FlipView>

推荐答案

我已经尝试过,并且可以按照您的描述访问 FlipView DataTemplate 内部的控件.尝试以下方法,如果这有帮助,请告诉我.

I have tried and I am able to access the control inside of FlipView DataTemplate as you described. Try to below approach and let me know if this helps.

public static IEnumerable<T> RecurseChildren<T>(DependencyObject root) where T : UIElement
{
    if (root is T)
    {
        yield return root as T;
    }

    if (root != null)
    {
        var count = VisualTreeHelper.GetChildrenCount(root);


        for (var idx = 0; idx < count; idx++)
        {
            foreach (var child in RecurseChildren<T>(VisualTreeHelper.GetChild(root, idx)))
            {
                yield return child;
            }
        }
    }
}

访问图像控件:

var imageControl = RecurseChildren<Image>(rootVisual).FirstOrDefault();

这里的 rootVisual 是我页面中的 Grid 实例.

here rootVisual is the Grid instance in my page.

这篇关于如何访问 FlipView 内 DataTemplate 内的 xaml 控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何访问 FlipView 内 DataTemplate 内的 xaml 控件

基础教程推荐