Wp7:获取ControlTemplate中的元素信息
发布日期:2021-09-08 22:54:43 浏览次数:19 分类:技术文章

本文共 2070 字,大约阅读时间需要 6 分钟。

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.NewFolder113 {14     public class Find:ContentControl15     {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         /// 42         /// 外部获取TextBox的值 43         /// 44         /// 
45 public string FindStr()46 {47 return findStr;48 }49 }50 }

Find.xaml

View Code
1 
5
28

在genera.xaml中添加路径:

1 

最后在页面中引用

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,读取其中的控件信息;

转载于:https://www.cnblogs.com/Yukang1989/archive/2012/11/16/2772823.html

转载地址:https://blog.csdn.net/weixin_34341229/article/details/93340769 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:日本語の文法
下一篇:数据库导出导入操作(expdp,impdp)

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年03月30日 03时49分38秒