How to add Custom Properties to WPF User Control

前端 未结 2 1926
花落未央
花落未央 2021-02-07 04:19

I\'ve my own User Control including a few buttons and etc.

I use this code to bring that UC to screen.



        
2条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-07 04:54

    You can use this declaration for your DependencyProperties:

    public bool Property1
    {
        get { return ( bool ) GetValue( Property1Property ); }
        set { SetValue( Property1Property, value ); }
    }
    
    // Using a DependencyProperty as the backing store for Property1.  
    // This enables animation, styling, binding, etc...
    public static readonly DependencyProperty Property1Property 
        = DependencyProperty.Register( 
              "Property1", 
              typeof( bool ), 
              typeof( XXXX ), 
              new PropertyMetadata( false ) 
          );
    

    This snippet can be found in Visual Studio if you type "propdp" and then TabTab. You'll need to fill the DependencyProperty's type, the name of the DependencyProperty, the class that contains it and the default value for that DependencyProperty (in my example, I put false as default).

提交回复
热议问题