这个标题我也不知道咋起,意思说一下你就明白。

1. 对官方控件的模板进行定制修改,以满足多样化需求,还有漂亮的UI

比如ListView,GridView等。

2. 在设计的情况下并没有这个控件,而在运行时的时候出现了它

比如微软的广告组件,他们叫AdControl,在运行时其实就是一个WebView

下面看一下我的实际项目中的代码,来举例说明:

<FlipView x:Name="flipView" Background="{ThemeResource SystemControlChromeMediumAcrylicWindowMediumBrush }">
<FlipView.ItemTemplate>
<DataTemplate>
<Grid>
<Image x:Name="myImage" Grid.RowSpan="3" Stretch="Uniform"
Source="{Binding img_realurl}" IsDoubleTapEnabled="True"
DoubleTapped="detailImage_DoubleTapped"/> <TextBlock Text="{Binding sitename}" Margin="3,0,0,0" VerticalAlignment="Center" Foreground="{ThemeResource SystemControlBackgroundAccentBrush}"/>
</StackPanel>
</Grid>
</DataTemplate>
</FlipView.ItemTemplate>
</FlipView>

我这个是定义的FlipView的模板,大家可以发现,里面用到个Image控件,而这个控件,你如果直接定义他的x:Name的话,在后台代码.cs里面使用myImage,是识别不到的。微软不让这么用。

那么怎么办,就是需要在运行时,通过代码查找他,然后再操作即可。

查找的方法如下:

        public static T MyFindListBoxChildOfType<T>(DependencyObject root) where T : class
{
var MyQueue = new Queue<DependencyObject>();
MyQueue.Enqueue(root);
while (MyQueue.Count > )
{
DependencyObject current = MyQueue.Dequeue();
for (int i = ; i < VisualTreeHelper.GetChildrenCount(current); i++)
{
var child = VisualTreeHelper.GetChild(current, i);
var typedChild = child as T;
if (typedChild != null)
{
return typedChild;
}
MyQueue.Enqueue(child);
}
}
return null;
}

然后在页面加载完成的事件里面使用,

private void Page_Loaded(object sender, RoutedEventArgs e)
{
Image headImage = MyFindListBoxChildOfType<Image>(flipView);
headImage.PointerEntered += Head_PointerEntered;
headImage.PointerExited += Head_PointerExited;
}

记下来就可以为所欲为的操作了。

有人说,我们的模板里有多个Image控件,咋办?

你将查找的函数改成返回List<T>即可,然后在Looaded里面按顺序取即可。

private void Page_Loaded(object sender, RoutedEventArgs e)
{
Image detailImage = MyFindListBoxChildOfType<Image>(flipView)[];
Image headImage = MyFindListBoxChildOfType<Image>(flipView)[];
}

这个顺序就是你在Xaml里面写的顺序。

UWP 查找模板中的控件的更多相关文章

  1. WPF备忘录(5)怎样修改模板中的控件

    首先,想问大家一个问题,你们如果要给一个Button添加背景图片会怎么做?(呵呵,这个问题又点小白哈) 是这样吗? <Button Height="57" Horizonta ...

  2. WPF关于控件 父级控件,子级控件,控件模板中的控件,等之间的相互访问

    原文:WPF关于控件 父级控件,子级控件,控件模板中的控件,等之间的相互访问 1,在菜单中访问 弹出菜单的控件 var mi = sender as MenuItem;//菜单条目 MenuItem ...

  3. WPF 获取控件模板中的控件

    DG是控件名称public T GetVisualChild<T>(DependencyObject parent, Func<T, bool> predicate) wher ...

  4. 在DataList、Repeater的HeaderTemplate和FooterTemplate模板中寻找控件FindControl

    [程序代码] <asp:Repeater ID="Repeater1" runat="server"> <HeaderTemplate> ...

  5. WPF日积月累之DataGrid样式以及操作数据模板中的控件

    一.效果图 二.代码预览 1 <Window x:Class="Test.MainWindow" 2 xmlns="http://schemas.microsoft ...

  6. UWP入门(五)--控件模板

    原文:UWP入门(五)--控件模板 通过在 XAML 框架中创建控件模板,你可以自定义控件的可视结构和可视行为(eg:勾选框的三种状态). 控件有多个属性,如 Background.Foregroun ...

  7. 关于在DataGrid.RowDetailsTemplate中的控件查找不到的问题

    DataGrid.RowDetailsTemplate中的控件需要显示出来才能查找,可以尝试在MouseLeftButtonUp等事件中处理.

  8. WindowsXamlHost:在 WPF 中使用 UWP 控件库中的控件

    在 WindowsXamlHost:在 WPF 中使用 UWP 的控件(Windows Community Toolkit) 一文中,我们说到了在 WPF 中引入简单的 UWP 控件以及相关的注意事项 ...

  9. dev中gridview控件 z

    目录:一.客户端常用1.常用API2.聚焦行变更事件3.客户端选择多行4.客户端选择行5. 获取选择的行数目6.单击行时,选中行7.通过checkbox 选择行8.选择所有行9.启动编辑框,Conta ...

随机推荐

  1. Add to Array-Form of Integer LT989

    For a non-negative integer X, the array-form of X is an array of its digits in left to right order.  ...

  2. Predict the Winner LT486

    Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from eith ...

  3. maven install 找不到符号问题

    看报错信息是找不到 javax.servlet 包 .这个是tomcat 内的jar包.但是我build path 查看是加了tomcat 的.. 最后在pom.xml 添加依赖 <depend ...

  4. MySQL把本地数据库上传到linux

    今天是要导入数据库到linux系统 先用Navicat把sql导出,然后登陆到linux去执行 登陆mysql: mysql -uroot -p123456 创建数据库:create database ...

  5. json(原生态)

    什么是 JSON ? JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation) JSON 是轻量级的文本数据交换格式 JSON 独立于语言 * JSO ...

  6. Syslog和Windows事件日志收集

    Syslog和Windows事件日志收集 EventLog Analyzer从分布式Windows设备收集事件日志,或从分布式Linux和UNIX设备.交换机和路由器(Cisco)收集syslog.事 ...

  7. GO介绍,环境的配置和安装 简单使用

    1. 介绍与安装 Golang 是什么 Go 亦称为 Golang(按照 Rob Pike 说法,语言叫做 Go,Golang 只是官方网站的网址),是由谷歌开发的一个开源的编译型的静态语言. Gol ...

  8. delphi 中如何处理“幽灵”、“熔断”?(转载)

    原始连接:http://dannywind.nl/delphi/meltdown-spectre-and-delphi/ Meltdown, Spectre and Delphi Don’t pani ...

  9. css初识和css选择器

    一.css是什么 css(cascading style sheet)定义如何显示HTML元素,给HTML设置样式,显得更为美观. 二.css的引入方式 1.行内引入 在标签中添加一个style是属性 ...

  10. Educational Codeforces Round 51 D. Bicolorings(dp)

    https://codeforces.com/contest/1051/problem/D 题意 一个2*n的矩阵,你可以用黑白格子去填充他,求联通块数目等于k的方案数,答案%998244353. 思 ...