locked
My custom controls in library do not work in applications RRS feed

  • Question

  • Sorry to ask such a simple question:

     

    I can't seem to use controls that I create in my Silverlight Control Library in other applications.  Here is my general process:

     

    1.  I create a new "Silverlight Control Library" in Expression Blend 4

    2.  I Create my controls... save.. and build.

    3.  I open a new "Silverlight Application + Webiste" in Expression Blend4

    4.  Right click on the project and add reference to the .dll

    5.  I drag my custom control onto the project..

     

    And it come up empty.  There is nothing there.  Why?

     

    Also.. when you make a custom control in a library... how do you add properties that will show up in the "Miscellaneous" area of Expression Blend's Properties window?

     

    Thanks

     

    Monday, March 28, 2011 6:11 PM

Answers

  • The code you have above doesn't match up.

     

    Changing the following:

    "ExpanderBackground" -> "MyProperty"

    public string MyProperty -> public SolidColorBrush MyProperty

    return (string)... -> return (SolidColorBrush)...

    Will make things match up. This DP/Property combo will show up in the Brushes category without any additional work.

     

    If you want to categorize it explicitly:

    public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register(
    			"MyProperty",                //Property name
    			typeof(SolidColorBrush),              //Property type
    			typeof(UserControl1),       //Type of the dependency property provider
    			  null);//Callback invoked on property value has changes
    
    		[System.ComponentModel.Description("This is the description text that shows in a tooltip.")]
    		[System.ComponentModel.Category("UserControl1 Category")]
    		public SolidColorBrush MyProperty
    		{
    			set
    			{
    				this.SetValue(MyPropertyProperty, value);
    			}
    
    			get
    			{
    				return (SolidColorBrush)this.GetValue(MyPropertyProperty);
    			}
    		}
    

     

     

    Tuesday, March 29, 2011 3:43 PM
    Moderator

All replies

  • Do you get any errors? Can you share a repro project?

    For the second question, both public properties and Dependency properties should show up under Miscellaneous.

    Monday, March 28, 2011 7:14 PM
    Moderator
  • For a better design time experience you could use attributes to locate your public properties and dependency properties in a special register tab.

    In order to use them you need to import system.componentmodel. Then you can use the Description("value") attribute to provide a description of the property. And use the Category("value") attribute. For example <Category("MyCustomControl")> _ will create a seperate register tab in Blend with the title "MyCustomControl" and the property to which you added this attribue will show up in this register tab.

    As value assigned to the Category attribute you can also use the name of one of the standard register tabs, e.g. "Layout" to show up your property in the "Layout" tab.

     

    Tuesday, March 29, 2011 4:20 AM
  • Thank You Martin.  I'm trying to move toward programming in such a way as to have all of my controls work (and be editable) in Expression Blend for a better design time experience (I've long been a code-behind only type of guy).

     

    Can you provide a line of code showing how to use the componentModel to place the following dependency propterty into Expression Blend?

            public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register(
                "ExpanderBackground",                               //Property name
                typeof( SolidColorBrush ),                           //Property type
                typeof( MapLowerThird  ),             //Type of the dependency property provider
                    new PropertyMetadata( MyPropertyChanged ) );//Callback invoked on property value has changes
           
        public string MyProperty
        {
            set

            {
                this.SetValue( MyPropertyProperty, value );
            }

            get
            {
                return ( string )this.GetValue( MyPropertyProperty );
            }
        }

     

    Thanks

     

     

    Tuesday, March 29, 2011 3:15 PM
  • The code you have above doesn't match up.

     

    Changing the following:

    "ExpanderBackground" -> "MyProperty"

    public string MyProperty -> public SolidColorBrush MyProperty

    return (string)... -> return (SolidColorBrush)...

    Will make things match up. This DP/Property combo will show up in the Brushes category without any additional work.

     

    If you want to categorize it explicitly:

    public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register(
    			"MyProperty",                //Property name
    			typeof(SolidColorBrush),              //Property type
    			typeof(UserControl1),       //Type of the dependency property provider
    			  null);//Callback invoked on property value has changes
    
    		[System.ComponentModel.Description("This is the description text that shows in a tooltip.")]
    		[System.ComponentModel.Category("UserControl1 Category")]
    		public SolidColorBrush MyProperty
    		{
    			set
    			{
    				this.SetValue(MyPropertyProperty, value);
    			}
    
    			get
    			{
    				return (SolidColorBrush)this.GetValue(MyPropertyProperty);
    			}
    		}
    

     

     

    Tuesday, March 29, 2011 3:43 PM
    Moderator
  • Thank You Chuck.  That did the trick!
    Tuesday, March 29, 2011 4:16 PM
  • Chuck,

     

    One follow up question:  When I add the above code to program.. it works great.  When I try to bind to the value by doing the following:

    Clicking on Data Binding.... => Element Property tab ... the dependency property (in this case "MyProperty" does not show up (or at least I can't find it).

    So.. how do I bind something to the new dp?

     

    Thanks

    Stephen

     

    Tuesday, March 29, 2011 4:47 PM
  • Woophs.. never mind... it is showing up  now.
    Tuesday, March 29, 2011 4:52 PM
  • Sorry, I was a bit to late to answer your reply.

    But for those VB guys with the same need, this is how it goes in VB:

    <Description("This is my Control description"), Category("My Control")> _
    Private Shared ReadOnly MyProperty As DependencyProperty ....
    

    Pretty much the same like in C#...

    Tuesday, March 29, 2011 4:59 PM