• WPF中的DataTemplate


    <Window x:Class="DateTemplate应用.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:DateTemplate应用"
            xmlns:collections="clr-namespace:System.Collections;assembly=mscorlib"
            Title="MainWindow" Height="350" Width="525">
        <Window.Resources>
            <!--DataTemplate-->
            <DataTemplate DataType="{x:Type local:Unit}">
                <Grid>
                    <StackPanel Orientation="Horizontal">
                        <Grid>
                            <Rectangle Stroke="Yellow" Fill="Orange" Width="{Binding Price}"/>
                            <TextBox Text="{Binding Year}"/>
                        </Grid>
                        <TextBlock Text="{Binding Price}" Margin="5,0"/>
                    </StackPanel>
                </Grid>
            </DataTemplate>
            <!--数据源-->
            <collections:ArrayList x:Key="ds">
                <local:Unit Year="2001" Price="100"/>
                <local:Unit Year="2002" Price="120"/>
                <local:Unit Year="2003" Price="140"/>
                <local:Unit Year="2004" Price="160"/>
                <local:Unit Year="2006" Price="180"/>
                <local:Unit Year="2008" Price="200"/>
            </collections:ArrayList>
        </Window.Resources>
        <StackPanel>
            <ListBox ItemsSource="{StaticResource ds}"/>
            <ComboBox ItemsSource="{StaticResource ds}"/>
        </StackPanel>
    </Window>

    以XML数据格式来显示

    <Window x:Class="XMLDataTemplate.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
            xmlns:xmlDataTemplate="clr-namespace:XMLDataTemplate"
            Title="MainWindow" Height="350" Width="525" WindowStartupLocation="CenterScreen">
        <Window.Resources>
            <!--DataTemplate-->
            <DataTemplate x:Key="MyXmlDataTemplate">
                <Grid>
                    <StackPanel Orientation="Horizontal">
                        <Grid>
                            <Rectangle Stroke="Yellow" Fill="Orange" Width="{Binding XPath=@Price}"/>
                            <TextBlock Text="{Binding XPath=@Year}"/>
                        </Grid>
                        <TextBlock Text="{Binding XPath=@Price}" Margin="5,0"/>
                    </StackPanel>
                </Grid>
            </DataTemplate>
            <!--数据源-->
            <XmlDataProvider x:Key="Ds"  XPath="Units/Unit">
                <x:XData>
                    <Units xmlns="">
                        <Unit Year="2001" Price="100"/>
                        <Unit Year="2002" Price="120"/>
                        <Unit Year="2001" Price="140"/>
                        <Unit Year="2001" Price="160"/>
                        <Unit Year="2001" Price="180"/>
                        <Unit Year="2001" Price="200"/>
                        <Unit Year="2001" Price="220"/>
                    </Units>
                </x:XData>
            </XmlDataProvider>
        </Window.Resources>
        <StackPanel>
            <ListBox ItemsSource="{Binding Source={StaticResource Ds}}" ItemTemplate="{StaticResource MyXmlDataTemplate}"/>
            <ComboBox ItemsSource="{Binding Source={StaticResource Ds}}" ItemTemplate="{StaticResource MyXmlDataTemplate}"/>
        </StackPanel>
    </Window>

     显示层级数据的模板是HierarchicaDataTemplate

    <?xml version="1.0" encoding="utf-8" ?>
    <Data xmlns="">
      <Grade Name="一年级">
        <Class Name="1班">
          <Group Name="A组"/>
          <Group Name="B组"/>
          <Group Name="C组"/>
          <Group Name="D组"/>
        </Class>
      </Grade>
      <Grade Name="二年级">
        <Class Name="1班">
          <Group Name="A组"/>
          <Group Name="B组"/>
          <Group Name="C组"/>
          <Group Name="D组"/>
        </Class>
      </Grade>
      <Grade Name="三年级">
        <Class Name="1班">
          <Group Name="A组"/>
          <Group Name="B组"/>
          <Group Name="C组"/>
          <Group Name="D组"/>
        </Class>
      </Grade>
    </Data>
    <Window x:Class="TreeView.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <Window.Resources>
            <!--数据源-->
            <XmlDataProvider x:Key="ds" Source="Data.xml" XPath="Data/Grade"/>
            <!--年级模板-->
            <HierarchicalDataTemplate DataType="Grade" ItemsSource="{Binding XPath=Class}">
                <TextBlock Text="{Binding XPath=@Name}"></TextBlock>
            </HierarchicalDataTemplate>
            <!--班级模板-->
            <HierarchicalDataTemplate DataType="Class" ItemsSource="{Binding XPath=Group}">
                <RadioButton Content="{Binding XPath=@Name}" GroupName="gn"></RadioButton>
            </HierarchicalDataTemplate>
            <!--小组模板-->
            <HierarchicalDataTemplate DataType="Group" ItemsSource="{Binding XPath=Student}">
                <CheckBox Content="{Binding XPath=@Name}"></CheckBox>
            </HierarchicalDataTemplate>
        </Window.Resources>
        <Grid>
            <TreeView ItemsSource="{Binding Source={StaticResource ds}}"></TreeView>
        </Grid>
    </Window>

  • 相关阅读:
    【Unity游戏开发】Android6.0以上的动态权限申请问题
    【游戏开发】网络编程之浅谈TCP粘包、拆包问题及其解决方案
    【Unity游戏开发】你真的了解UGUI中的IPointerClickHandler吗?
    【Unity游戏开发】记一次解决 LuaFunction has been disposed 的bug的过程
    【年终总结】马三北漂记之8102年总结
    【小白学Lua】之Lua变长参数和unpack函数
    【计算机图形学】计算机图形学中的坐标系统
    【Unity游戏开发】tolua之wrap文件的原理与使用
    【游戏开发】基于VS2017的OpenGL开发环境搭建
    【Unity游戏开发】用C#和Lua实现Unity中的事件分发机制EventDispatcher
  • 原文地址:https://www.cnblogs.com/chenyongblog/p/3569972.html
Copyright © 2020-2023  润新知