WPF VisualTreeHelper的使用
<Window x:Class="MyWpf.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:c="clr-namespace:System.Collections;assembly=mscorlib"
xmlns:local="clr-namespace:MyWpf"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<c:ArrayList x:Key="ps">
<local:Person Id="1" Name="zhangsan" HasJob="True" skill="wpf"/>
<local:Person Id="2" Name="lisi" HasJob="False" skill="C#"/>
<local:Person Id="3" Name="wangwu" HasJob="True" skill="wpf"/>
<local:Person Id="4" Name="maliu" HasJob="False" skill="C#"/>
</c:ArrayList>
<DataTemplate x:Key="IdTemp">
<TextBlock Text="{Binding Id}"></TextBlock>
</DataTemplate>
<DataTemplate x:Key="NameTemp">
<TextBox Text="{Binding Name}" GotFocus="Name_GotFocus"></TextBox>
</DataTemplate>
<DataTemplate x:Key="SkillTemp">
<TextBox Text="{Binding skill}"></TextBox>
</DataTemplate>
<DataTemplate x:Key="HasJobTemp">
<CheckBox IsChecked="{Binding HasJob}" Name="checkboxHasJob"></CheckBox>
</DataTemplate>
</Window.Resources>
<Grid>
<ListView ItemsSource="{StaticResource ps}" Name="lv">
<ListView.View>
<GridView>
<GridViewColumn Width="100" Header="Id" CellTemplate="{StaticResource IdTemp}"></GridViewColumn>
<GridViewColumn Width="100" Header="Name" CellTemplate="{StaticResource NameTemp}"></GridViewColumn>
<GridViewColumn Width="100" Header="Skill" CellTemplate="{StaticResource SkillTemp}"></GridViewColumn>
<GridViewColumn Width="100" Header="HasJob" CellTemplate="{StaticResource HasJobTemp}"></GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace MyWpf
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
List<Person> lst = new List<Person>()
{
new Person(){ Id=1, Name="zhangsan", HasJob=false, skill="C#"},
new Person(){ Id=2, Name="lisi", HasJob=true, skill="C#"},
new Person(){ Id=3, Name="wangwu", HasJob=false, skill="wpf"},
new Person(){ Id=4, Name="maliu", HasJob=true, skill="C#"},
new Person(){ Id=5, Name="zhaoqi", HasJob=false, skill="wpf"},
};
}
private void Name_GotFocus(object sender, RoutedEventArgs e)
{
TextBox tb = e.OriginalSource as TextBox;
ContentPresenter cp = tb.TemplatedParent as ContentPresenter;
Person p = cp.Content as Person;
lv.SelectedItem = p;
ListViewItem lvi = lv.ItemContainerGenerator.ContainerFromItem(p) as ListViewItem;
CheckBox cb = FindVisualChild<CheckBox>(lvi);
MessageBox.Show(cb.Name);
}
private ChildType FindVisualChild<ChildType>(DependencyObject obj) where ChildType : DependencyObject
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(obj, i);
if (child != null && child is ChildType)
{
return child as ChildType;
}
else
{
ChildType childOfChild = FindVisualChild<ChildType>(child);
if (childOfChild!=null)
{
return childOfChild;
}
}
}
return null;
}
}
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public string skill { get; set; }
public bool HasJob { get; set; }
}
}
WPF VisualTreeHelper的使用的更多相关文章
- WPF中的VisualTreeHelper
VisualTreeHelper提供了一组WPF控件树模型,通过VisualTreeHelper可以遍历控件的树形结构,获取我们所需要的控件以及相应的属性: VisualTreeHelper提供了一下 ...
- WPF之ComboBox的VisualTreeHelper
原文:WPF之ComboBox的VisualTreeHelper 用WPF的ComboBox控件的时候,需要用到TextChanged属性,但是这个属性属于TextBox控件,不用担心,ComboBo ...
- WPF利用VisualTreeHelper遍历寻找对象的子级对象或者父级对象
原文:WPF利用VisualTreeHelper遍历寻找对象的子级对象或者父级对象 简介 本文将完整叙述我利用VisualTreeHelper实现题述功能的全部过程,想直接看函数实现的朋友可以跳到函数 ...
- wpf通过VisualTreeHelper找到控件内所有CheckBox和TextBox并做相应绑定
#region CheckBox与TextBox绑定 Dictionary<CheckBox, TextBox> CheckTextBoxDic = new Dictionary<C ...
- WPF ContextMenu+VisualTreeHelper实现删除控件操作
<UserControl MouseRightButtonDown="UserControl_MouseRightButtonDown" > <UserC ...
- WPF之VisualTreeHelper
/// <summary> /// </summary> /// <typeparam name="T">< ...
- WPF中Visible设为Collapse时,VisualTreeHelper.GetChildrenCount为0
今天遇到一个奇怪的问题, 在给一个控件内的子元素绑定事件时,失败. 发现原因是,这个控件初始化时Visible="Collapse",这时控件内的可视树就没有生成.导致绑定事件失败 ...
- 2000条你应知的WPF小姿势 基础篇<45-50 Visual Tree&Logic Tree 附带两个小工具>
在正文开始之前需要介绍一个人:Sean Sexton. 来自明尼苏达双城的软件工程师.最为出色的是他维护了两个博客:2,000Things You Should Know About C# 和 2,0 ...
- Problem of saving images in WPF (RenderTargetBitmap)zz
To save a visual to an image file need to use RenderTargetBitmap, detail is reference to Save and ...
随机推荐
- python 标准库 —— io(StringIO)
0. io流(io stream) 流是一种抽象概念,它代表了数据的无结构化传递.按照流的方式进行输入输出,数据被当成无结构的字节序或字符序列.从流中取得数据的操作称为提取操作,而向流中添加数据的操作 ...
- 嵌入式linux串口编程(二)
/*com_writer.c*/#include "uart_api.h" int main(void){ int fd; char buff[BUFFER_SIZE]; if(( ...
- 【u245】机房病毒
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 我们机房中了病毒,因此几乎什么都无法正常进入.为了解决这个病毒,我们花了好几天.终于在大家的共同努力下 ...
- Java 学习(22):Java MySQL 连接
Java MySQL 连接 本章节我们为大家介绍 Java 如何使用 使用 JDBC 连接 MySQL 数据库. Java 连接 MySQL 需要驱动包,最新版下载地址为:http://dev.mys ...
- Jcaptca 图片
http://www.oschina.net/code/snippet_89964_6009 http://blog.csdn.net/tender001/article/details/846969 ...
- udp网络程序-发送、接收数据
1. udp网络程序-发送数据 创建一个基于udp的网络程序流程很简单,具体步骤如下: 创建客户端套接字 发送/接收数据 关闭套接字 代码如下: #coding=utf-8from socket im ...
- iptables 重启系统生效
1. 重启系统生效 开启: chkconfig iptables on 关闭: chkconfig iptables off 2. 即时生效,重启后失效 开启: service iptables ...
- QQ欢乐斗地主心得体会 (三):高倍场攻略
上个月,也可能是上个月的上个月,我的欢乐豆兵积攒到了百万之众,突破了历史记录.最近2个月一直在高倍场混,想写点高倍场的心得体会.本篇的体会,与以前写的2篇斗地主心得体会,有点不同. 特别说明:我主要玩 ...
- Expression Blend 的点滴(4)--创建类似iPhone屏幕锁控件(上)
原文:Expression Blend 的点滴(4)--创建类似iPhone屏幕锁控件(上) 本篇文章,最终效果图: 当然,不只是一个UI而已,如果只是一张图片,那专业的设计师能做出更出色的效果.在 ...
- 在jsp页面里面设置全局引用文件
head.jsp文件 将项目中所需要用到次数比较多的的插件,库等,同意放在一个jsp文件里面,命名为head.jsp文件,相当于一个全局的 <%@ page language="jav ...