20

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

I use this code to bring that UC to screen.

<AppUI:XXXX x:Name="ucStaticBtns" HorizontalAlignment="Left" Margin="484,0,0,0" VerticalAlignment="Top" Width="68" />

I've added two property like Property1 and Property2 to XXXX user control. And changed my code with

<AppUI:XXXX x:Name="ucStaticBtns" HorizontalAlignment="Left" Margin="484,0,0,0" VerticalAlignment="Top" Width="68" Property1="False" Property2="False"/>

When I add this 2 parameters to XAML page, system throws an exception like "The member 'Property1' is not recognized or is not accessible"

Here is my UC code.

 public partial class XXXX : UserControl
    {
        public event EventHandler CloseClicked;
        public event EventHandler MinimizeClicked;
        //public bool ShowMinimize { get; set; }
        public static DependencyProperty Property1Property;
        public static DependencyProperty Property2Property;
        public XXXX()
        {
            InitializeComponent();
        }

        static XXXX()
        {
            Property1Property = DependencyProperty.Register("Property1", typeof(bool), typeof(XXXX));
            Property2Property = DependencyProperty.Register("Property2", typeof(bool), typeof(XXXX));
        }

        public bool Property1
        {
            get { return (bool)base.GetValue(Property1Property); }
            set { base.SetValue(Property1Property, value); }
        }

        public bool Property2
        {
            get { return (bool)base.GetValue(Property2Property); }
            set { base.SetValue(Property2Property, value); }
        }
}

Can you help me with doing that? Thank you so much!

2 Answers 2

56

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).

Sign up to request clarification or add additional context in comments.

3 Comments

The way VS adapts the generated code fragment (by means of propdp) to my modifications is magic. Do you know of a “cheat sheet” showing more of this?
After spending days wrestling data context problems, I took a step back and started again using this simple example. Pure gold.
It surprises me that XAML doesn't have a way to do this. I would have thought <sys:Boolean x:Name="Property1" /> or some such would be a much cleaner way of developing controls.
3

You may not have declared your DependencyPropertys correctly. You can find out full details about how to create DependencyPropertys in the Dependency Properties Overview page on MSDN, but in short, they look something like this (taken from the linked page):

public static readonly DependencyProperty IsSpinningProperty = 
    DependencyProperty.Register(
    "IsSpinning", typeof(Boolean),
...
    );

public bool IsSpinning
{
    get { return (bool)GetValue(IsSpinningProperty); }
    set { SetValue(IsSpinningProperty, value); }
}

You can find further help in the DependencyProperty Class page on MSDN.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.