UWP 查找模板中的控件
这个标题我也不知道咋起,意思说一下你就明白。
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 查找模板中的控件的更多相关文章
- WPF备忘录(5)怎样修改模板中的控件
首先,想问大家一个问题,你们如果要给一个Button添加背景图片会怎么做?(呵呵,这个问题又点小白哈) 是这样吗? <Button Height="57" Horizonta ...
- WPF关于控件 父级控件,子级控件,控件模板中的控件,等之间的相互访问
原文:WPF关于控件 父级控件,子级控件,控件模板中的控件,等之间的相互访问 1,在菜单中访问 弹出菜单的控件 var mi = sender as MenuItem;//菜单条目 MenuItem ...
- WPF 获取控件模板中的控件
DG是控件名称public T GetVisualChild<T>(DependencyObject parent, Func<T, bool> predicate) wher ...
- 在DataList、Repeater的HeaderTemplate和FooterTemplate模板中寻找控件FindControl
[程序代码] <asp:Repeater ID="Repeater1" runat="server"> <HeaderTemplate> ...
- WPF日积月累之DataGrid样式以及操作数据模板中的控件
一.效果图 二.代码预览 1 <Window x:Class="Test.MainWindow" 2 xmlns="http://schemas.microsoft ...
- UWP入门(五)--控件模板
原文:UWP入门(五)--控件模板 通过在 XAML 框架中创建控件模板,你可以自定义控件的可视结构和可视行为(eg:勾选框的三种状态). 控件有多个属性,如 Background.Foregroun ...
- 关于在DataGrid.RowDetailsTemplate中的控件查找不到的问题
DataGrid.RowDetailsTemplate中的控件需要显示出来才能查找,可以尝试在MouseLeftButtonUp等事件中处理.
- WindowsXamlHost:在 WPF 中使用 UWP 控件库中的控件
在 WindowsXamlHost:在 WPF 中使用 UWP 的控件(Windows Community Toolkit) 一文中,我们说到了在 WPF 中引入简单的 UWP 控件以及相关的注意事项 ...
- dev中gridview控件 z
目录:一.客户端常用1.常用API2.聚焦行变更事件3.客户端选择多行4.客户端选择行5. 获取选择的行数目6.单击行时,选中行7.通过checkbox 选择行8.选择所有行9.启动编辑框,Conta ...
随机推荐
- 一台老服务器的升级日志(PHP4.4.9+MySQL5.1)升级MySQL5.6
1.备份数据库 mysqldump -uroot -p --all-databases > databases.sql 2.停止mysql服务 service mysqld stop 3.卸载旧 ...
- 常见CSS
.login_top_bg { background-image: url(/pcssc/images/login/login-top-bg.gif); background-repeat: repe ...
- 844. Backspace String Compare
class Solution { public: bool backspaceCompare(string S, string T) { int szs=S.size(); int szt=T.siz ...
- spring boot中注入jpa时报could not autowire.No beans of 'PersonRepository' type found
解决方法,在repository加一个注解.如下图所示: @Component
- Graphviz 环境变量设置
今天晚上解决了一个错误,如下:
- windows下解决端口被占用的问题
步骤一.Windows查看所有的端口 点击电脑左下角的开始,然后选择运行选项,接着我们在弹出的窗口中,输入[cmd]命令,进行命令提示符.然后我们在窗口中输入[netstat -ano]按下回车,即会 ...
- Booksim的运行
1.下载flex和bison解压到/home/cp/booksim2-master/src; 2.进入flex.bison的目录下分别执行: ./configure make make install ...
- TCP粘包问题分析和解决(全)
TCP通信粘包问题分析和解决(全) 在socket网络程序中,TCP和UDP分别是面向连接和非面向连接的.因此TCP的socket编程,收发两端(客户端和服务器端)都要有成对的socket,因此,发送 ...
- C++STL deque
deque双端数组 deque<int> dq; deque<int>::iterator it; dq.push_back();//尾部插入元素 dq.push_front( ...
- oracle学习笔记一:用户管理(3)用户口令管理
当某个用户不断的尝试密码进行登录数据库是很危险的,因此对密码(口令)的管理十分重要.好在我们可以限制登录次数,超过某些次数的登录将会把用户锁住,隔一段时间才允许其登录,这和你的手机是不是有点一样,你的 ...