WPF .cs中使用资源文件中的ControlTemplate或Style并找到控件

一、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 
            
        }
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值