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