MSDN資料範本化概觀的練習

  • 792
  • 0

摘要:MSDN資料範本化概觀的練習

MSDN位置:http://msdn.microsoft.com/zh-tw/library/ms742521(v=vs.100).aspx

使用 DataTrigger 套用屬性值

C#

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows.Media.Imaging;

namespace 資料範本化概觀
{
    public enum TTaskType { Home, Work };
    public class Task
    {
        public string TaskName { get; set; }
        public string Description { get; set; }
        public int Priority { get; set; }
        public TTaskType TaskType { get; set; }
        public BitmapImage Img { get; set; }
        public Task(string taskname, string description, int priority, TTaskType tasktype)
        {
            TaskName = taskname;
            Description = description;
            Priority = priority;
            TaskType = tasktype;
            Img = new BitmapImage(new Uri("h.png", UriKind.RelativeOrAbsolute));
        }

        public override string ToString()
        {
            return TaskName;
        }
    }

    public class Tasks : ObservableCollection
    {
        public Tasks()
        {
            Add(new Task("Task 1", "This is Task 1", 0, TTaskType.Home));
            Add(new Task("Task 2", "This is Task 2", 1, TTaskType.Work));
        }
    }


}

XAML:

<Window x:Class="資料範本化概觀.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="clr-namespace:資料範本化概觀"
        Title="Introduction to Data Templating Sample"
        Height="350" Width="525">
    <Window.Resources>
        <DataTemplate DataType="{x:Type vm:Task}">
            <Border Name="border" BorderBrush="Aqua" BorderThickness="5" Padding="5" Margin="5">
            <DockPanel LastChildFill="True">
                <DockPanel DockPanel.Dock="Left">
                    <Image Width="32" Height="32" Source="{Binding Img}"/>
                </DockPanel>
                <StackPanel>                
                    <TextBlock Text="{Binding Path=TaskName}"/>
                    <TextBlock Text="{Binding Path=Description}"/>
                    <TextBlock Text="{Binding Path=Priority}"/>
                    <TextBlock Text="{Binding Path=GTaskType}"/>
                </StackPanel>
            </DockPanel>
            </Border>
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding Path=TaskType}">
                    <DataTrigger.Value>
                        <vm:TTaskType>Home</vm:TTaskType>
                    </DataTrigger.Value>
                    <Setter TargetName="border" Property="BorderBrush" Value="Yellow"/>
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
        <DataTemplate x:Key="templateaaaa">            
            <StackPanel>
                <TextBlock Text="{Binding Path=TaskName}"/>
                <TextBlock Text="{Binding Path=Description}"/>
                <TextBlock Text="{Binding Path=Priority}"/>
                <TextBlock Text="{Binding Path=GTaskType}"/>
            </StackPanel>
        </DataTemplate>
    </Window.Resources>
    <Window.DataContext>
        <vm:Tasks/>
    </Window.DataContext>
    <StackPanel>
        <TextBlock>My Task List:</TextBlock>
        <ComboBox ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True" HorizontalContentAlignment="Stretch">
        </ComboBox>
        <ContentControl Content="{Binding}" ContentTemplate="{StaticResource templateaaaa}"/>        
    </StackPanel>
</Window>