WPF 寻找数据模板中的元素
<Window x:Class="Wpf180706.Window11"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Wpf180706"
Title="Window11" Height="300" Width="300">
<Window.Resources>
<local:Person x:Key="person" Id="1" Name="zhangsan" Skill="wpf"></local:Person>
<DataTemplate x:Key="tpl">
<Border BorderBrush="Orange" BorderThickness="3" CornerRadius="5">
<StackPanel>
<TextBlock Text="{Binding Id}"></TextBlock>
<TextBlock Name="tb2" Text="{Binding Name}"></TextBlock>
<TextBlock Text="{Binding Skill}"></TextBlock>
</StackPanel>
</Border>
</DataTemplate>
</Window.Resources>
<Grid>
<StackPanel>
<ContentPresenter Name="cp" Content="{StaticResource person}" ContentTemplate="{StaticResource tpl}"></ContentPresenter>
<Button Click="btn_Click">btn</Button>
</StackPanel>
</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.Shapes;
namespace Wpf180706
{
/// <summary>
/// Interaction logic for Window11.xaml
/// </summary>
public partial class Window11 : Window
{
public Window11()
{
InitializeComponent();
}
private void btn_Click(object sender, RoutedEventArgs e)
{
TextBlock tb = (TextBlock)this.cp.ContentTemplate.FindName("tb2", this.cp);
MessageBox.Show(tb.Text);
}
}
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public string Skill { get; set; }
}
}
WPF 寻找数据模板中的元素的更多相关文章
- WPF 基础 - 在模板中找元素
1. 在 ControlTemplate 中寻找元素 <Window.Resources> <ControlTemplate x:Key="cTmp"> & ...
- WPF数据模板中绑定事件不触发问题
今天比较闲,做一个练手的项目,结果在xaml中写了一个用户的数据模板后,在其中的某个Canvas上绑定了一个鼠标左击的事件,结果调试的时候,无论怎么点击都不跳到断点那里,百思不得其解. 之后尝试不绑定 ...
- 继续聊WPF——动态数据模板
我为啥称之为“动态数据模板”?先看看下面的截图,今天,我们就是要实现这种功能. 大概是这样的,我们定义的DataTemplate是通过触发器动态应用到 ComboBoxItem 上. 这个下拉列表控件 ...
- WPF 寻找控件模板中的元素
<Window x:Class="Wpf180706.Window10" xmlns="http://schemas.microsoft.com/wi ...
- 背水一战 Windows 10 (76) - 控件(控件基类): Control - 基础知识, 焦点相关, 运行时获取 ControlTemplate 和 DataTemplate 中的元素
[源码下载] 背水一战 Windows 10 (76) - 控件(控件基类): Control - 基础知识, 焦点相关, 运行时获取 ControlTemplate 和 DataTemplate 中 ...
- WPF教程十五:数据模板的使用(重发)
数据模板 数据模板是一段如何显示绑定在VM对象的XAML代码.数据模板可以包含任意元素的组合,基于Binding来显示不同的信息. 在实际的开发中数据模板的应用场景很多,同样一个控件可以根据不同的绑定 ...
- 转载 WPF -- 控件模板 (ControlTemplate)(一) https://blog.csdn.net/qq_23018459/article/details/79899838
ControlTemplate(控件模板) https://blog.csdn.net/qq_23018459/article/details/79899838 WPF包含数据模板和控件模板,其中 ...
- dev Gridcontrol单元格值格式化及在模板中调用命令
<dxg:GridColumn> <dxg:GridColumn.EditSettings> ...
- ListBox项模板中绑定ListBoxItem属性的方法
原文:ListBox项模板中绑定ListBoxItem属性的方法 <ListBox> <ListBox.ItemTemplate> <DataTemplate> & ...
随机推荐
- 小强的HTML5移动开发之路(53)——jQueryMobile页面间参数传递
在单页模版中使用基于HTTP的方式通过POST和GET请求传递参数,而在多页模版中不需要与服务器进行通信,通常在多页模版中有以下三种方法来实现页面间的参数传递. 1.GET方式:在前一个页面生成参数并 ...
- Xcode经常使用插件使用及自己主动生成帮助文档
*一.Xcode 插件下载:* VVDocumenter下载:https://github.com/onevcat/VVDocumenter-Xcode Xcode经常使用插件下载:http://pa ...
- 【codeforces 754A】Lesha and array splitting
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【t075】郁闷的记者
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 你是一个体育报社的记者,你接受到一个艰难的任务:有N支足球队参加足球比赛,现在给你一些比赛的结果,需要 ...
- UItableview正在滚动的时候进行操作容易出问题
tableview正在滚动的时候进行其它点击事件操作容易出问题,有时候会出现莫名其妙的数组越界的bug, 解决方法:1.对objectatindex方法进行异常判断 2. [_tableview se ...
- [.NET Core 24]把project.json迁移到.csproj
链接:https://blog.jetbrains.com/dotnet/2017/04/04/rider-eap-update-csproj-based-net-core-support-migra ...
- Nginx与真实IP
配置了Nginx,Tomcat中的Web程序,获得的ip一直是"127.0.0.1",比较纳闷.获得远程ip,已经判断了很多情况,为什么会这样呢? 正解 proxy_set_hea ...
- 跟上 Java 8 – 了解 lambda
原文出处: 王爵nice 从java8出现以来lambda是最重要的特性之一,它可以让我们用简洁流畅的代码完成一个功能. 很长一段时间java被吐槽是冗余和缺乏函数式编程能力的语言,随着函数式编程的流 ...
- 解决Request method 'GET' not supported问题
博主最近遇到了这个问题,解决情况如下 第一种情况:前台页面的表单在一些情况下没有指定POST方法: Ajax没有指定POST方法: 后台方法在一定情况下需要指定POST方法: 第二种情况:前端参数类型 ...
- sql server中触发器
触发器是一种特殊类型的存储过程,它不同于之前的我们介绍的存储过程.触发器主要是通过事件进行触发被自动调用执行的.而存储过程可以通过存储过程的名称被调用. Ø 什么是触发器 触发器对表进行插入.更新.删 ...