zoukankan      html  css  js  c++  java
  • WPF 寻找控件模板中的元素

    <Window x:Class="Wpf180706.Window10"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="Window10" Height="300" Width="300">
        <Window.Resources>
            <ControlTemplate x:Key="temp">
                <StackPanel Background="Yellow">
                    <TextBox Name="txt1" Margin="6"></TextBox>
                    <TextBox Name="txt2" Margin="6"></TextBox>
                    <TextBox Name="txt3" Margin="6"></TextBox>
                </StackPanel>
                
            </ControlTemplate>
        </Window.Resources>
        
        <Grid>
            <StackPanel>
                <UserControl Name="uc" Template="{StaticResource temp}"></UserControl>
                <Button Click="btn_Click">btn</Button>
            </StackPanel>
        </Grid>

    </Window>

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;


    namespace Wpf180706
    {
        /// <summary>
        /// Interaction logic for Window10.xaml
        /// </summary>
        public partial class Window10 : Window
        {
            public Window10()
            {
                InitializeComponent();
            }


            private void btn_Click(object sender, RoutedEventArgs e)
            {
                TextBox txt = (TextBox)this.uc.Template.FindName("txt1",this.uc);
                txt.Text = "Hello";
                StackPanel sp = txt.Parent as StackPanel;
                ((TextBox)sp.Children[1]).Text = "Hello2";
                ((TextBox)sp.Children[2]).Text = "Hello3";
            }
        }
    }

  • 相关阅读:
    结果填空:青蛙爬井
    天上的星星 (前缀和)
    Poj3253 Fence Repair (优先队列)
    Requests+BeautifulSoup+正则表达式爬取猫眼电影Top100(名称,演员,评分,封面,上映时间,简介)
    数字图像处理之几种滤波器
    CodeForces
    直方图部分
    Codeforces Round #431 (Div. 2)
    2017中国大学生程序设计竞赛
    C++中数字与字符串之间的转换(转载自http://www.cnblogs.com/luxiaoxun/)
  • 原文地址:https://www.cnblogs.com/Jeely/p/11076266.html
Copyright © 2011-2022 走看看