WPF Gets Controls and Values of Control Templates

WPF Gets Controls and Values of Control Templates

Suppose we define the control template in WPF, Button has a control Textbox generated by Template and its x:name= TextVBox1 in the template. They do not conflict, but Button will not see the details of the control inside the control. The internal elements of the control will not pay attention to what is outside the control. If we want to access the controls inside Button, how do we get the attribute values? Do it? In fact, WPF has prepared access to code for internal controls.
ControlTemplate and DataTemplate generated controls are Template-generated controls. Both ControalTemplate and DataTemplate classes are derived from the Framework Template class. This class has a method named FindNamede for us to retrieve internal controls. That is to say, we can get Template to find its internal controls without problems. For ControlTemplate objects, access to its target controls The Template attribute is available.
First, we look for the control generated by ControlTemplate. First, we design a ControlTemplate and apply it to UserControl. There is also a Button on the interface. In its click event processing, we retrieve the code generated by ControlTemplate.

1. The interface XAML code is as follows:

<Window x:Class="Wpf.FindName"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Wpf"
        mc:Ignorable="d"
        Title="FindName" Height="450" Width="800">
    ----------Resources--------->
<Window.Resources>
   ------Control template------->
        <ControlTemplate x:Key="cTemp">
            <StackPanel Background="Orange">
                <TextBox Margin="6" x:Name="TextBox1"></TextBox>
                <TextBox Margin="6" x:Name="TextBox2"></TextBox>
                <TextBox Margin="6" x:Name="TextBox3"></TextBox>
            </StackPanel>
        </ControlTemplate>
    </Window.Resources>
    <StackPanel Background="Yellow">
        <UserControl x:Name="uc" Template="{StaticResource cTemp}"></UserControl>
        <Button Content="FindName" Height="30" Width="120" Click="Button_Click"></Button>
        <Button Content="GetValues" Height="30" Width="120" Margin="10,10" Click="Button_Click1"></Button>
    </StackPanel>
</Window>

2. Button Click Event Background Processing Code

 //FileNmae clicks to assign values to template controls
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //"uc" is the Name value of the UserControl control added to the xaml form
            //Find the control whose Name is TextBox 1 under the template of the control whose Name is "uc"
            TextBox tb = this.uc.Template.FindName("TextBox1", this.uc) as TextBox;
            tb.Text = "I can find my name!";//Assign a value to the control
            StackPanel sp = tb.Parent as StackPanel;//Find the control whose parent (parent) of the tb control is StackPanel
            //Assign values to two subitems of sq
            (sp.Children[1] as TextBox).Text = "I can find my name too!";
            (sp.Children[2] as TextBox).Text = "So easy!";
        }
        //GetValues Gets Template Control Values
        private void Button_Click1(object sender, RoutedEventArgs e)
        {
            //"uc" is the Name value of the UserControl control added to the xaml form
            //Gets the specified TextBox object under the control whose Name is uc
            TextBox tb1 = this.uc.Template.FindName("TextBox1", this.uc) as TextBox;
            TextBox tb2 = this.uc.Template.FindName("TextBox2", this.uc) as TextBox;
            TextBox tb3 = this.uc.Template.FindName("TextBox3", this.uc) as TextBox;
            //Pop up the prompt box to get the value
            MessageBox.Show("TextBox1 The value is:" + tb1.Text.ToString() + ",TextBox2 The value is:" + tb2.Text.ToString() + ",TextBox3 The value is:" + tb3.Text.ToString());
        }

Page results:

Click on the button to assign a value to the object

Click on the button to get the object value

Tags: Attribute

Posted on Mon, 07 Oct 2019 21:00:34 -0700 by Danestar