首页 > 其他 > 详细

WPF-控件-DataTemplate生成的控件

时间:2014-02-28 15:21:43      阅读:546      评论:0      收藏:0      [点我收藏+]

bubuko.com,布布扣

bubuko.com,布布扣
<Window x:Class="由DataTemplate生成的控件.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:由DataTemplate生成的控件"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <!--数据对象-->
        <local:Student x:Key="stu" Id="1" Name="张三" Skill="C#" HasJob="True"/>
        <!--DataTemplate-->
        <DataTemplate x:Key="stuDT">
            <Border BorderBrush="Orange" BorderThickness="2" CornerRadius="5">
                <StackPanel>
                    <TextBlock Text="{Binding Id}" Margin="5"/>
                    <TextBlock x:Name="textBlockName" Text="{Binding Name}" Margin="5"/>
                    <TextBlock Text="{Binding Skill}" Margin="5"/>
                </StackPanel>
            </Border>
        </DataTemplate>
    </Window.Resources>
    <StackPanel>
        <ContentPresenter x:Name="cp" Content="{StaticResource stu}" ContentTemplate="{StaticResource stuDT}" Margin="5"/>
        <Button Content="Find" Margin="5,0" Click="ButtonBase_OnClick"></Button>
    </StackPanel>
</Window>
bubuko.com,布布扣
bubuko.com,布布扣
        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            TextBlock tb = this.cp.ContentTemplate.FindName("textBlockName", this.cp) as TextBlock;
            MessageBox.Show(tb.Text);
        }
bubuko.com,布布扣
bubuko.com,布布扣
    class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Skill { get; set; }
        public bool HasJob { get; set; }
    }
bubuko.com,布布扣

WPF-控件-DataTemplate生成的控件,布布扣,bubuko.com

WPF-控件-DataTemplate生成的控件

原文:http://www.cnblogs.com/chenyongblog/p/3572497.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!