How to access xaml Control inside DataTemplate inside FlipView(如何访问 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 控件
基础教程推荐
- 将 XML 转换为通用列表 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- c# Math.Sqrt 实现 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01