2010年12月9日 星期四

Silverlight TreeView 的 DataBinding (2)

簡介

WPF TreeView 的 DataBinding (2) 相同的問題,仍然發生在 Silverlight 上。那 Silverlight 可以使用與 WPF 相同的解法嗎?不行,原因出在Silverlight 並設計出 DataTemplateSelector 這樣的概念。

但是,相同的問題該怎麼解決呢?

自己做一個 DataTemplateSelector

沒有?就自己打造一個。

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

namespace SilverlightTreeView1
{
    public abstract class DataTemplateSelector : ContentControl
    {
        public virtual DataTemplate SelectTemplate(
                object item, DependencyObject container)
        {
            return null;
        }

        protected override void OnContentChanged(
                object oldContent, object newContent)
        {
            base.OnContentChanged(oldContent, newContent);

            ContentTemplate = SelectTemplate(newContent, this);
        }
    }
    
    public class MyDataTemplateSelector : DataTemplateSelector
    {
        public DataTemplate FileTemplate { get; set; }
        public HierarchicalDataTemplate FolderTemplate { get; set; }
        public override System.Windows.DataTemplate SelectTemplate(object item, System.Windows.DependencyObject container)
        {
            if (item is MyFolder)
                return FolderTemplate;
            else
                return FileTemplate;
        }
    }
}

xaml

<UserControl x:Class="SilverlightTreeView1.MainPage"
        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"
        mc:Ignorable="d"
        d:DesignHeight="300" d:DesignWidth="400" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" xmlns:SilverlightTreeView1="clr-namespace:SilverlightTreeView1">
    <UserControl.Resources>
        <SilverlightTreeView1:FolderHelper x:Key="helper" />
        <sdk:HierarchicalDataTemplate x:Key="folder" ItemsSource="{Binding Items}">
            <StackPanel Orientation="Horizontal">
                <Image Source="/SilverlightTreeView1;component/Images/folder.png" />
                <TextBlock Text="{Binding Name}" />
            </StackPanel>
        </sdk:HierarchicalDataTemplate>
        <sdk:HierarchicalDataTemplate x:Key="file">
            <StackPanel Orientation="Horizontal">
                <Image Source="/SilverlightTreeView1;component/Images/doc.png" />
                <TextBlock Text="{Binding Name}" />
            </StackPanel>
        </sdk:HierarchicalDataTemplate>
    </UserControl.Resources>
        <Grid x:Name="LayoutRoot" Background="White">
        <sdk:TreeView Name="treeView1" ItemsSource="{Binding Source={StaticResource helper}, Path=Items}">
            <sdk:TreeView.ItemTemplate>
                <sdk:HierarchicalDataTemplate ItemsSource="{Binding Items}">
                    <SilverlightTreeView1:MyDataTemplateSelector Content="{Binding}"
                        FolderTemplate="{StaticResource folder}" FileTemplate="{StaticResource file}" />
                </sdk:HierarchicalDataTemplate>
            </sdk:TreeView.ItemTemplate>
        </sdk:TreeView>
    </Grid>
</UserControl>

程式運行結果

image

程式碼下載

沒有留言:

Share with Facebook