WPF 寻找控件模板中的元素
<Window x:Class="Wpf180706.Window10"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window10" Height="300" Width="300">
<Window.Resources>
<ControlTemplate x:Key="temp">
<StackPanel Background="Yellow">
<TextBox Name="txt1" Margin="6"></TextBox>
<TextBox Name="txt2" Margin="6"></TextBox>
<TextBox Name="txt3" Margin="6"></TextBox>
</StackPanel>
</ControlTemplate>
</Window.Resources>
<Grid>
<StackPanel>
<UserControl Name="uc" Template="{StaticResource temp}"></UserControl>
<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 Window10.xaml
/// </summary>
public partial class Window10 : Window
{
public Window10()
{
InitializeComponent();
}
private void btn_Click(object sender, RoutedEventArgs e)
{
TextBox txt = (TextBox)this.uc.Template.FindName("txt1",this.uc);
txt.Text = "Hello";
StackPanel sp = txt.Parent as StackPanel;
((TextBox)sp.Children[1]).Text = "Hello2";
((TextBox)sp.Children[2]).Text = "Hello3";
}
}
}
最新文章
- Creating Signing Identities 生成签名标识
- 终端 git log 修改样式
- 【项目】iOS - 使用UIWebView占用内存过大
- HDU 4704 Sum (高精度+快速幂+费马小定理+二项式定理)
- Jackson - Date Handling
- UVA 11916 Emoogle Grid 离散对数 大步小步算法
- 数据库(学习整理)----4--Oracle数据查询(基础点1)
- Android Fragment 嵌套使用报错
- 项目管理实践【五】自动编译和发布网站【Using Visual Studio with Source Control System to build and publish website automatically】
- 原生js 实现 Ajax 跨浏览器使用
- css雪碧图(css splite)
- TurnipBit—MicroPython开发板:从积木式编程语言开始学做小小创客
- [个人项目] echarts 实现数据(tooltip)自动轮播插件
- SpringMVC系列(五)使用 Serlvet 原生的 API 作为目标方法的参数
- eclipse maven jetty启动修改默认端口
- DWG/DGN格式导入Arcgis;转化为shp格式;更改地理坐标;导入Google Earth【转】
- Let Her Go
- 20145302张薇《Java程序设计》第五周学习总结
- 一款连接SqlServer的数据库工具
- hdu 3835:R(N)(水题,数学题)