How to hide a part of a WPF control

前端 未结 4 1294
梦如初夏
梦如初夏 2021-02-02 03:30

Is it possible to hide a part of a WPF control? .NET 4 has a DatePicker which renders 4 parts, according to MSDN. Is it possible to hide (in my case) the TextBox part (probably

4条回答
  •  太阳男子
    2021-02-02 04:33

    You can do it all in XAML with behaviors and the TemplatePartAction I wrote:

        
            
                
                    
                        
                    
                
            
        
    

    and here is the TemplatePart functionality:

    public class TemplatePartHelper where T : IAttachedObject
    {
        public IAttachedObject Parent { get; set; }
        public List Children { get; private set; }
        public string Name { get; set; }
        public Control TemplatePart { get; set; }
    
        public TemplatePartHelper(IAttachedObject parent)
        {
            Parent = parent;
            Children = new List();
        }
    
        public void AssociateChildren()
        {
            if (TemplatePart != null)
                return;
            var control = Parent.AssociatedObject as Control;
            var template = control.Template;
            if (template == null)
                return;
            var partName = "PART_" + Name;
            TemplatePart = template.FindName(partName, control) as Control;
            if (TemplatePart == null)
                return;
            foreach (var child in Children)
                child.Attach(TemplatePart);
        }
    }
    
    [ContentProperty("Behaviors")]
    public class TemplatePartBehavior : Behavior
    {
        public TemplatePartHelper Helper { get; private set; }
        public List Behaviors { get { return Helper.Children; } }
        public string TemplatePartName { get { return Helper.Name; } set { Helper.Name = value; } }
    
        public TemplatePartBehavior()
        {
            Helper = new TemplatePartHelper(this);
        }
    
        protected override void OnAttached()
        {
            AssociatedObject.Initialized += (s, e) => Helper.AssociateChildren();
            AssociatedObject.Loaded += (s, e) => Helper.AssociateChildren();
        }
    }
    
    [ContentProperty("Actions")]
    public class TemplatePartAction : TriggerAction
    {
        public TemplatePartHelper Helper { get; private set; }
        public List Actions { get { return Helper.Children; } }
        public string TemplatePartName { get { return Helper.Name; } set { Helper.Name = value; } }
    
        public TemplatePartAction()
        {
            Helper = new TemplatePartHelper(this);
        }
    
        protected override void OnAttached()
        {
            AssociatedObject.Loaded += (s, e) => Helper.AssociateChildren();
        }
    
        protected override void Invoke(object parameter)
        {
            foreach (var action in Actions)
            {
                if (action.IsEnabled)
                {
                    var methodInfo = typeof(TriggerAction).GetMethod("Invoke", BindingFlags.Instance | BindingFlags.NonPublic, null, new Type[] { typeof(object) }, null);
                    methodInfo.Invoke(action, new object[] { parameter });
                }
            }
        }
    }
    

    If you are not familiar with behaviors, Install the Expression Blend 4 SDK and add these namespaces:

    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
    xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
    

    and add System.Windows.Interactivity and Microsoft.Expression.Interactions to your project.

提交回复
热议问题