コード例 #1
0
        public override bool ApplyTemplate()
        {
            if (this.IsInitialized && this.visualChild == null)
            {
                Visual visual = this.Content as Visual;

                if (visual == null && this.Content != null)
                {
                    DataTemplate template = this.ContentTemplate;

                    if (template == null)
                    {
                        DataTemplateKey key = new DataTemplateKey(this.Content.GetType());
                        template = this.TryFindResource(key) as DataTemplate;
                    }

                    if (template != null)
                    {
                        visual = template.CreateVisualTree(this);

                        FrameworkElement fe = visual as FrameworkElement;

                        if (fe != null)
                        {
                            fe.DataContext = this.Content;
                        }
                    }
                    else
                    {
                        visual = new TextBlock
                        {
                            Text = this.Content.ToString(),
                        };
                    }
                }

                if (visual != null)
                {
                    this.visualChild = visual;
                    this.AddVisualChild(this.visualChild);
                    this.OnApplyTemplate();
                    return(true);
                }
            }

            return(false);
        }