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 ...
随机推荐
- Android 节日短信送祝福(功能篇:2-短信历史记录Fragment的编写)
因为用于展示短信记录的是一个ListView,但是为了方便,可以直接继承自ListFragment,就可以免去写ListView对应的布局了,只需要写其item对应的布局即可. item_sended ...
- php 过滤js字符串代码
$str = '<script type="text/javascript" src="dd.js"></script> 测试php正则 ...
- centos-mirrors
http://mirrors.aliyun.com/centos/7.2.1511/os/x86_64/Packages/ http://mirrors.aliyun.com/centos/7.2.1 ...
- [Django] ModelViewSet from rest_framework and Router
To build rest api easily, we can use ModelViewSet from rest_framework. It provides GET, POST, DELETE ...
- bash keys
stty-a CTRL-S and CTRL-Q are called flow-control characters. Bash readline 使用技巧_Linux编程_操作系统_希赛网 htt ...
- Cocos2d-x 3.0final 终结者系列教程05-AppDelegate入口类
下面是Cocos2d-x的程序入口: class AppDelegate : private cocos2d::Application { public: AppDelegate(); virtua ...
- Dubbo服务框架解析(二)
本节介绍dubbo-common,dubbo-common是公共逻辑模块,包含Util类.通用模型,是其他模块的基础. 扩展机制 SPI SPI是扩展点的注解.标注在类型上.全部的扩展点须要通过SPI ...
- Spring学习笔记之六(数据源的配置)
1.前言 上一篇博客分析了,Spring中实现AOP的两种动态代理的机制,以下这篇博客.来解说一下Spring中的数据源的配置. 2.DAO支持的模板类 Spring提供了非常多关于Dao支持的模板 ...
- 最新版Butterknife plugin支持butterknife7.0.1和兼容butterknife 6.1.0及下面
JakeWharton 的butterknife帮我们有效的攻克了findViewById及各种view的监听事件泛滥的问题,极大的简化了代码,假设使用了android Studio开发的配上avas ...
- Java设计模式菜鸟系列(二十二)中介者模式建模与实现
转载请注明出处:http://blog.csdn.net/lhy_ycu/article/details/40027109 中介者模式(Mediator):主要用来减少类与类之间的耦合的,由于假设类与 ...