WPF调用模板

WPF调用模板
在这里插入图片描述
前台代码

<Window x:Class="Target.MainWindow"
        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:Target"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style TargetType="{x:Type TreeViewItem}">
            <Setter Property="Foreground" Value="Blue"/>
            <Setter Property="FontSize" Value="12"/>
        </Style>

        <Style TargetType="{x:Type TextBox}">
            <Setter Property="Width" Value="80"/>
            <Setter Property="TextWrapping" Value="Wrap"/>
        </Style>
        <XmlDataProvider x:Key="MyEmployeeData" XPath="/EmployeeData">
            <x:XData>
                <EmployeeData xmlns="">
                    <EmployeeInfo>
                        <EmployeeInfoData>Employee1</EmployeeInfoData>
                        <Item Type="FirstName">Jesper</Item>
                        <Item Type="LastName">Aaberg</Item>
                        <Item Type="EmployeeNumber">12345</Item>
                    </EmployeeInfo>
                    <EmployeeInfo>
                        <EmployeeInfoData>Employee2</EmployeeInfoData>
                        <Item Type="FirstName">Dominik</Item>
                        <Item Type="LastName">Paiha</Item>
                        <Item Type="EmployeeNumber">98765</Item>
                    </EmployeeInfo>
                </EmployeeData>
            </x:XData>
        </XmlDataProvider>

        <HierarchicalDataTemplate DataType="EmployeeInfo" 
      ItemsSource ="{Binding XPath=Item}">
            <TextBlock Text="{Binding XPath=EmployeeInfoData}" />
        </HierarchicalDataTemplate>

    </Window.Resources>
    <StackPanel>
        <TextBlock>
        TreeView control with TreeViewItem content explicitly 
        defined as Header strings</TextBlock>
        <TreeView>
            <TreeViewItem Header="Employee1">
                <TreeViewItem Header="Jesper"/>
                <TreeViewItem Header="Aaberg"/>
                <TreeViewItem Header="12345"/>
            </TreeViewItem>
            <TreeViewItem Header="Employee2">
                <TreeViewItem Header="Dominik"/>
                <TreeViewItem Header="Paiha"/>
                <TreeViewItem Header="98765"/>
            </TreeViewItem>
        </TreeView>

        <TextBlock>
        TreeView control with leaf nodes specified as
        Items that are Buttons
        </TextBlock>
        <TreeView>
            <TreeViewItem Header ="Employee1">
                <TreeViewItem.Items>
                    <Button Click="OnClick">Jesper</Button>
                    <Button Click="OnClick">Aaberg</Button>
                    <Button Click="OnClick">12345</Button>
                </TreeViewItem.Items>
            </TreeViewItem>
            <TreeViewItem Header="Employee2">
                <TreeViewItem.Items>
                    <Button Click="OnClick">Dominik</Button>
                    <Button Click="OnClick">Paiha</Button>
                    <Button Click="OnClick">98765</Button>
                </TreeViewItem.Items>
            </TreeViewItem>
        </TreeView>
        <TextBlock>
          TreeView control with root nodes
          defined as Buttons and and leaf nodes
          that are Header strings
        </TextBlock>
        <TreeView>
            <TreeViewItem>
                <TreeViewItem.Header>
                    <Button Click="OnClick">Employee1</Button>
                </TreeViewItem.Header>
                <TreeViewItem Header="Jesper"/>
                <TreeViewItem Header="Aaberg"/>
                <TreeViewItem Header="12345"/>
            </TreeViewItem>
            <TreeViewItem>
                <TreeViewItem.Header>
                    <Button Click="OnClick">Employee2</Button>
                </TreeViewItem.Header>
                <TreeViewItem Header="Dominik"/>
                <TreeViewItem Header="Paiha"/>
                <TreeViewItem Header="98765"/>
            </TreeViewItem>
        </TreeView>
        <TextBlock>
        TreeView control created
        by databinding with an XMLDataProvider and using a 
        HierarchicalDataTemplate
        </TextBlock>
        <TreeView ItemsSource="{Binding Source={StaticResource MyEmployeeData}, 
        XPath=EmployeeInfo}"/>
        <TextBlock>
       TreeView control whose Items contain DockPanel controls
       with embedded Image and TextBlock content
        </TextBlock>
        <TreeView>
            <TreeViewItem Header="Animals">
                <TreeViewItem.Items>
                    <DockPanel>
                        <Image Source="data\fish.png"/>
                        <TextBlock Margin="5" Foreground="Brown"
                       FontSize="12">Fish</TextBlock>
                    </DockPanel>
                    <DockPanel>
                        <Image Source="data\dog.png"/>
                        <TextBlock Margin="5" Foreground="Brown"
                       FontSize="12">Dog</TextBlock>
                    </DockPanel>
                    <DockPanel>
                        <Image Source="data\cat.png"/>
                        <TextBlock Margin="5" Foreground="Brown"
                       FontSize="12">Cat</TextBlock>
                    </DockPanel>
                </TreeViewItem.Items>
            </TreeViewItem>
        </TreeView>
    </StackPanel>
</Window>

后台代码

using System.Windows;
using System.Windows.Controls;

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

        private void OnClick(object sender, RoutedEventArgs e)
        {
            var targetButton = sender as Button;
            MessageBox.Show(targetButton?.Content + " invoked.");
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

向着光-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值