当前位置:网站首页>WPF .cs中使用资源文件中的ControlTemplate或Style并找到控件
WPF .cs中使用资源文件中的ControlTemplate或Style并找到控件
2022-08-03 19:36:00 【爱编程的小菜媛】
一、Generic.xaml中:
<ControlTemplate x:Key="ucQuitFeeCT" TargetType="{x:Type localControls:ucQuitFee}">
<Grid Grid.Row="1">
<TabControl Grid.Row="0" x:Name="neuTabControl1" Style="{DynamicResource tabControlStyle}">
<TabItem Header="退费(F2)" IsSelected="True" x:Name="tpQuit" Style="{DynamicResource tabItemStyle}">
<GroupBox Header="基本信息">
<DockPanel>
<TextBlock Width="55" Margin="5 0" VerticalAlignment="Center" >发票号:</TextBlock>
<!--举例寻找这个控件-->
<TextBox x:Name="tbInvoiceNO" Width="132" VerticalAlignment="Center"></TextBox>
</DockPanel>
</GroupBox>
</TabItem>
</TabControl>
</Grid>
</ControlTemplate>
二、.cs中
#region 声明控件
protected TextBox tbInvoiceNO = new TextBox();
#endregion
ControlTemplate thisControlTemplate = null;
public void SetControlTemplate()
{
ResourceDictionary resourceDictionary = new ResourceDictionary();
resourceDictionary.Source = new Uri("/程序集;component/Themes/Generic.xaml", UriKind.RelativeOrAbsolute);
this.Resources.MergedDictionaries.Add(resourceDictionary);
thisControlTemplate = (ControlTemplate)this.Resources["ucQuitFeeCT"];
this.Template = thisControlTemplate;
}
//得到控件
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
#region 控件赋值
tbInvoiceNO = (TextBox)thisControlTemplate.FindName("tbInvoiceNO", this);
#endregion
#region 绑定控件事件
#endregion
}
边栏推荐
猜你喜欢
随机推荐
【微信小程序】NFC 标签打开小程序
阿里巴巴政委体系-第七章、阿里政委培育
FreeRTOS中级篇
Standard C language learning summary 11
傅里叶变换(深入浅出)
盲僧发现了华点——教你如何使用API接口获取数据
C中的数据存储
Network protocol-TCP, UDP difference and TCP three-way handshake, four wave
Postgresql-xl global snapshot and GTM code walking (branch line)
MySQL读写分离的三种实现方案
力扣刷题之有效的正方形(每日一题7/29)
设备树基本原理与操作方法
MySQL 主从,6 分钟带你掌握!
Jingdong cloud released a new generation of distributed database StarDB 5.0
Solution for no navigation bar after Word is saved as PDF
docker mysql 容器中执行mysql脚本文件并解决乱码
JS 内置构造函数 扩展 prototype 继承 借用构造函数 组合式 原型式creat 寄生式 寄生组合式 call apply instanceof
Redis 内存满了怎么办?这样置才正确!
ECCV 2022 Oral | 满分论文!视频实例分割新SOTA: IDOL
力扣刷题之数组序号计算(每日一题7/28)