zoukankan      html  css  js  c++  java
  • 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; }
        }
    }

  • 相关阅读:
    jPoint开发简介
    使用.NET程序集作为Business Data Connectivity数据源(一)
    SharePoint2010内容类型剖析(一)
    SharePoint Learning Kit核心内容概述(四)
    SharePoint 2010正式版发布
    推荐一份极好的SharePoint 2010开发资料
    使用jQuery为数据视图添加图表显示
    SharePoint Designer 2010的新增功能
    SharePoint Learning Kit核心内容概述(三)
    SharePoint2010内容类型剖析(三)
  • 原文地址:https://www.cnblogs.com/dxmfans/p/9434617.html
Copyright © 2011-2022 走看看