TemplatePart属性的确切用途是什么? [英] What is exact use of TemplatePart Attribute?

查看:203
本文介绍了TemplatePart属性的确切用途是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了自定义控件,并且某些网站使用了TemplatePart属性,例如以下代码段,

I developed custom control and some of sites used TemplatePart attribute like the below code snippet,

[TemplatePart(Name = "Main", Type = typeof(Border))]
    [TemplatePart(Name = "body", Type = typeof(ContentControl))]
    public class CustomControl1 : Control
    {
        static CustomControl1()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl1), new FrameworkPropertyMetadata(typeof(CustomControl1)));
        }

        public CustomControl1()
        {
            //EventManager.RegisterClassHandler(typeof(CustomControl1), Keyboard.KeyUpEvent, new MouseButtonEventHandler(OnMouseUp));
        }

     
        static TextBlock control;
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();           
            control = base.GetTemplateChild("body") as TextBlock;
            
        }



        public string Content
        {
            get { return (string)GetValue(ContentProperty); }
            set { SetValue(ContentProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Content.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ContentProperty =
            DependencyProperty.Register("Content", typeof(string), typeof(CustomControl1), new PropertyMetadata(null,new PropertyChangedCallback(MyCustom_PropertyChanged)));

        private static void MyCustom_PropertyChanged(DependencyObject dobj, DependencyPropertyChangedEventArgs e)
        {
            control.Text = e.NewValue.ToString();

            if (e.NewValue.ToString() != e.OldValue)
            {
                CustomControl1 CusControl = (CustomControl1)dobj;
                CusControl.RaiseContentChanged();
            }
        }

        public static readonly RoutedEvent ContentChangedEvent = EventManager.RegisterRoutedEvent("ContentChanged", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(CustomControl1));

        public event RoutedEventHandler ContentChanged
        {
            add { AddHandler(ContentChangedEvent, value); }
            remove { RemoveHandler(ContentChangedEvent, value); }
        }

        private void RaiseContentChanged()
        {
            RoutedEventArgs args = new RoutedEventArgs(ContentChangedEvent);
            RaiseEvent(args);
        }
    }

为什么我们需要TemplatePart 属性 .没有此属性,将无法从后面的代码访问模板部分.请让我知道确切的目的

Why we need the TemplatePart  Attribute . Without this attribute am able to access template part from code behind. Please let me know the exact purpose

推荐答案

有一则由 Jeff Wilcox 撰写的博客文章.

There is a blog post written by Jeff Wilcox.

模板部件和自定义控件(快速提示)

还有关于代码项目的另一篇文章.

And also another post on code project.

如何访问控制来自代码隐藏"的模板部分

我认为这可能对您有所帮助.

I think it might be helpful to you.


这篇关于TemplatePart属性的确切用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆