DataTemplate


.使用DataTemplateSelector

 // Summary:
    //     Provides a way to choose a System.Windows.DataTemplate based on the data
    //     object and the data-bound element.

 

创建一个继承该类的子类,在SelectTemplate方法中指定使用哪个DataTemplate

 public class PlaceTemplateSelector : DataTemplateSelector
    {
        private DataTemplate washingtonTemplate;

        public DataTemplate WashingtonTemplate
        {
            get { return washingtonTemplate; }
            set { washingtonTemplate = value; }
        }

        private DataTemplate notWashingtonTemplate;

        public DataTemplate NotWashingtonTemplate
        {
            get { return notWashingtonTemplate; }
            set { notWashingtonTemplate = value; }
        }

        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            Place place = (Place)item;

            if (place.State == "WA")
            {
                return washingtonTemplate;
            }
            else
            {
                return notWashingtonTemplate;
            }
        }
    }

SDK中可以找到该类的使用方法:WPFSamples\ConnectedData\DataTemplatingIntro

 

Beatriz Costa在他的博客中也对此做了详细的介绍:http://www.beacosta.com/Archive/2006_01_01_bcosta_archive.html

posted on 2006-12-28 21:43  stswordman  阅读(397)  评论(0编辑  收藏  举报