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 ...
随机推荐
- php随机字符串
<?php class RandChar{ function getRandChar($length){ $str = null; $strPol = "ABCDEFGHIJKLMNO ...
- USB 3.0规范中译本 第2章 术语及缩略语
本文为CoryXie原创译文,转载及有任何问题请联系cory.xie#gmail.com. 本章列出并定义本规范通篇将使用的术语及缩略语. 术语/略缩语 定义 ACK(确认包) 表示积极肯定的握手包. ...
- 《从零開始学Swift》学习笔记(Day 71)——Swift与C/C++混合编程之数据类型映射
原创文章.欢迎转载.转载请注明:关东升的博客 posted @ 2017-07-21 13:23 zhchoutai 阅读(...) 评论(...) 编辑 收藏
- FullPage.js全屏滚动插件解说
1.主要功能 1).支持鼠标滚动 2).多个回调函数 3).支持手机.平板触屏事件 4).支持css3动画 5).支持窗口缩放 6).窗口缩放时自动调整 7).可设置滚动宽度.背景颜色.滚动速度.循环 ...
- Python科学计算(一)
一.准备教材:<python科学计算> 作者: 张若愚 出版社: 清华大学出版社 出版年: 2012-1 页数: 621 定价: 98.00元 装帧: 平装 ISBN: 97873022 ...
- BZOJ 1369 Gem - 树型dp
传送门 题目大意: 给一棵树上每个点一个正权值,要求父子的权值不同,问该树的最小权值和是多少. 题目分析: 证不出来最少染色数,那就直接信仰用20来dp吧:dp[u][i]表示u节点权值赋为i时u子树 ...
- [GeekBand] 设计模式之观察者模式学习笔记
本文参考文献::GeekBand课堂内容,授课老师:李建忠 :网络资料: http://blog.csdn.net/hguisu/article/details/7556625 本文仅作为自己的学习笔 ...
- FPGA综合优化
1 速度和面积 在全面优化水平将达到速度和面积RTL要利用逻辑拓扑的优势. 供FPGA由于在后端而言缺乏知识,门级优化.普通情况下更高的速度要求更高的并行性以及更大的面积,可是在某些特殊情况下并非这样 ...
- 字符串、对象、数组操作方法、json方法
1.字符串操作方法 1.charAt * 作用 * 通过索引找字符 ...
- 将普通java工程转换成maven工程
在MyEclipse10中将Maven项目转成普通Java项目后,想将Java项目转成Maven项目,结果一下子傻眼了.根本就没有攻略中提到的config标签.仔细一看,喵咪的,人家用的是Eclips ...