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

你可能感兴趣的:(template)