WPF Expander Style 控件样式 自定义控件 自定义属性

下载连接 https://download.csdn.net/download/sinat_30224769/12366122

 

Wpf中 使用 Expander 的样式 ,如果需要绑定自己的属性可以继承Expander 并 添加自己需要的属性 ,如果属性较多可以只绑定一个类,注意增加通知。 最后在样式中使用TempleteBinding 来帮顶自己设置的值,不要忘了 要设置Templete的 X:TargetType 继承的类

WPF Expander Style 控件样式 自定义控件 自定义属性_第1张图片

样式 资源DefaultStyleDictionary.xaml  我这里只改了一个Down情况的 因为只用到这一种,如果需要对应其他方向的,自己照葫芦画瓢


    


    
    
    
    
    
    
    
    
    
    
    
    

    
    
    
    
    
    
    

    
    
    
    
    
    

实现自定义属性 自定义控件

 



    public class User1Expander : Expander
    {
        public User1Expander()
        {

        }

        public string Title
        {
            get { return (string)GetValue(TitleProperty); }
            set { SetValue(TitleProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Title.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TitleProperty =
            DependencyProperty.Register("Title", typeof(string), typeof(User1Expander), new PropertyMetadata("标题"));

        public bool Status
        {
            get { return (bool)GetValue(StatusProperty); }
            set { SetValue(StatusProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Status.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty StatusProperty =
            DependencyProperty.Register("Status", typeof(bool), typeof(User1Expander), new PropertyMetadata(false));

        public string Message
        {
            get { return (string)GetValue(MessageProperty); }
            set { SetValue(MessageProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Message.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MessageProperty =
            DependencyProperty.Register("Message", typeof(string), typeof(User1Expander), new PropertyMetadata("未填写"));



  public class User1ToggleButton : ToggleButton
    {

        public string Title
        {
            get { return (string)GetValue(TitleProperty); }
            set { SetValue(TitleProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Title.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TitleProperty =
            DependencyProperty.Register("Title", typeof(string), typeof(User1ToggleButton), new PropertyMetadata("标题"));

        public bool Status
        {
            get { return (bool)GetValue(StatusProperty); }
            set { SetValue(StatusProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Status.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty StatusProperty =
            DependencyProperty.Register("Status", typeof(bool), typeof(User1ToggleButton), new PropertyMetadata(false));

        public string Message
        {
            get { return (string)GetValue(MessageProperty); }
            set { SetValue(MessageProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Message.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MessageProperty =
            DependencyProperty.Register("Message", typeof(string), typeof(User1ToggleButton), new PropertyMetadata("未填写"));



    }

最后 Main

调用



    
 
            
                
                
            
        
    
    

        
            
                
            
            
                
            
            
                
            
            
            
            
        

    

 

你可能感兴趣的:(技术学习系列,小功能Demo,小功能,代码片段)