• Wp7:获取ControlTemplate中的元素信息


    Find.cs类

    View Code
     1 using System;
     2 using System.Net;
     3 using System.Windows;
     4 using System.Windows.Controls;
     5 using System.Windows.Documents;
     6 using System.Windows.Ink;
     7 using System.Windows.Input;
     8 using System.Windows.Media;
     9 using System.Windows.Media.Animation;
    10 using System.Windows.Shapes;
    11 
    12 namespace CustomController.controller.NewFolder1
    13 {
    14     public class Find:ContentControl
    15     {
    16         private string findStr = string.Empty;
    17         private TextBox testTextBox;
    18         private Button testButton;
    19 
    20         public Find()
    21         {
    22             this.DefaultStyleKey = typeof(Find);
    23         }
    24 
    25         public override void OnApplyTemplate()
    26         {
    27             base.OnApplyTemplate();
    28 
    29             this.testTextBox = GetTemplateChild("testTextBox") as TextBox;
    30             this.testButton = GetTemplateChild("testButton") as Button;
    31 
    32             this.testTextBox.Text = "测试一哈,获取或设置ContentControl中元素的值";
    33 
    34             this.testButton.Click += delegate(object sender, RoutedEventArgs e)
    35             {
    36                 MessageBox.Show(this.testTextBox.Text);
    37                 findStr = this.testTextBox.Text;
    38             };
    39         }
    40 
    41         /// <summary>
    42         /// 外部获取TextBox的值 
    43         /// </summary>
    44         /// <returns></returns>
    45         public string FindStr()
    46         {
    47             return findStr;
    48         }
    49     }
    50 }

    Find.xaml

    View Code
     1 <ResourceDictionary 
     2                    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     3                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     4                    xmlns:local="clr-namespace:CustomController.controller.NewFolder1">
     5     <Style TargetType="local:Find">
     6         <Setter Property="Template">
     7             <Setter.Value>
     8                 <ControlTemplate TargetType="local:Find">
     9     
    10                     <Grid HorizontalAlignment="Stretch"
    11                           VerticalAlignment="Stretch">
    12                         <Grid.RowDefinitions>
    13                             <RowDefinition></RowDefinition>
    14                             <RowDefinition></RowDefinition>
    15                         </Grid.RowDefinitions>
    16 
    17                         <TextBox x:Name="testTextBox"
    18                                  Grid.Row="0"></TextBox>
    19                         <Button x:Name="testButton"
    20                                     Grid.Row="1"
    21                                     Content="测试"></Button>
    22                     </Grid>
    23                    
    24                 </ControlTemplate>
    25             </Setter.Value>
    26         </Setter>
    27     </Style>
    28 </ResourceDictionary>

    在genera.xaml中添加路径:

    1 <ResourceDictionary Source="/CustomController;component/controller/NewFolder1/Find.Theme.xaml"/>

    最后在页面中引用

     1    public MainPage()
     2         {
     3             InitializeComponent();
     4 
     5             Find contentControl = new Find();
     6 
     7             Button button = new Button();
     8             button.Width = 260;
     9             button.Height = 250;
    10             button.Content = contentControl;
    11 
    12             this.LayoutRoot.Children.Add(button);
    13 
    14             if (contentControl.FindStr().Trim().Length > 0)
    15                 MessageBox.Show(contentControl.FindStr());
    16         }

    这样就可以实现,自定义的ControlTemplate,读取其中的控件信息;

  • 相关阅读:
    Textarea自适应文字内容调整高度
    把页面某内容放入粘贴板中
    通过javamail发送电子邮件
    Jrebel+tomcat实现热部署
    Eclipse启动Tomcat时,45秒超时解决方式
    mybatis 多对多 处理
    单例模式
    Centos6安装mysql5.7
    maven手动导入jar包到本地仓库
    Jsp与servlet本质上的区别
  • 原文地址:https://www.cnblogs.com/Yukang1989/p/2772823.html
Copyright © 2020-2023  润新知