• WPF 寻找数据模板中的元素


    <Window x:Class="Wpf180706.Window11"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:Wpf180706"
            Title="Window11" Height="300" Width="300">
        <Window.Resources>
            <local:Person x:Key="person" Id="1" Name="zhangsan" Skill="wpf"></local:Person>
            <DataTemplate x:Key="tpl">
                <Border BorderBrush="Orange" BorderThickness="3" CornerRadius="5">
                    <StackPanel>
                        <TextBlock Text="{Binding Id}"></TextBlock>
                        <TextBlock Name="tb2" Text="{Binding Name}"></TextBlock>
                        <TextBlock Text="{Binding Skill}"></TextBlock>
                    </StackPanel>
                </Border>
            </DataTemplate>
        </Window.Resources>
        <Grid>
            <StackPanel>
            <ContentPresenter Name="cp" Content="{StaticResource person}" ContentTemplate="{StaticResource tpl}"></ContentPresenter>
            <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 Window11.xaml
        /// </summary>
        public partial class Window11 : Window
        {
            public Window11()
            {
                InitializeComponent();
            }


            private void btn_Click(object sender, RoutedEventArgs e)
            {
               TextBlock tb =  (TextBlock)this.cp.ContentTemplate.FindName("tb2", this.cp);
               MessageBox.Show(tb.Text);
            }


        }


        public class Person
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public string Skill { get; set; }
        }
    }

  • 相关阅读:
    【bzoj3782】上学路线 dp+容斥原理+Lucas定理+中国剩余定理
    【bzoj3210】花神的浇花集会 旋转坐标系
    【bzoj3513】[MUTC2013]idiots FFT
    【bzoj1043】[HAOI2008]下落的圆盘 计算几何
    【bzoj2521】[Shoi2010]最小生成树 网络流最小割
    【bzoj4811】[Ynoi2017]由乃的OJ 树链剖分+线段树区间合并
    【bzoj2467】[中山市选2010]生成树 矩阵树定理
    【bzoj1002】[FJOI2007]轮状病毒 矩阵树定理+高精度
    【bzoj4031】[HEOI2015]小Z的房间 矩阵树定理
    【bzoj4292】[PA2015]Równanie 暴力
  • 原文地址:https://www.cnblogs.com/dxmfans/p/9434617.html
Copyright © 2020-2023  润新知