locked
Creating a collection Dependency Property in a custom user control RRS feed

  • Question

  • Hi all-

    Can someone point me to a tutorial on creating a dependency property which is a collection of items?  I am able to do this using the following code, but ideally, for simplification, I'd like to remove the need to have a LinksCollection (in the example below), but have my links fall under the .Links property (just seems like I have one extra level that is not needed for some reason).  In the end, I am trying to allow a user to use blend and add items to the .Links as children.

     

    Thanks for any help.

     

    XAML:

     

    <local:HelpTopic Title="ARK Online Tutorials">
                            <local:HelpTopic.Links>
                                <local:LinksCollection>
                                    <local:NavigationLink LinkName="Overview" LinkAddress="http://www.yahoo.com" FontStyle="Italic" />
                                    <local:NavigationLink LinkName="Interface Navigation" LinkAddress="http://www.yahoo.com" />
                                    <local:NavigationLink LinkName="Modules" LinkAddress="http://www.yahoo.com" />
                                    <local:NavigationLink LinkName="Test Drive" LinkAddress="http://www.yahoo.com" />
                                 </local:LinksCollection>
                            </local:HelpTopic.Links>
                        </local:HelpTopic>

     

     

     

     

    C#

     

     

    public class LinksCollection : ObservableCollection<Object>
        {
        }
    
    private LinksCollection links;
    
    
    
    
    public HelpTopic()
    
    {
    			this.InitializeComponent();
    
    			// Insert code required on object creation below this point.
    
                
    
    		}
    
    // Setup a Dependency Property Collection for 
            // creating nested user controls within the 
            // HelpTopic user control
    
            #region Links Dependency Property
            
                public LinksCollection Links
                {
                    get
                    {
                        return (LinksCollection)GetValue(LinksProperty);
                    }
                    set
                    {
                        SetValue(LinksProperty, value);
                    }
                }
     
                public static readonly DependencyProperty LinksProperty =
                        DependencyProperty.Register(
                        "Links",
                        typeof(LinksCollection),
                        typeof(HelpTopic),
                        new FrameworkPropertyMetadata(
                        new PropertyChangedCallback(ChangeLinks)));
    
            
                private static void ChangeLinks(DependencyObject source, DependencyPropertyChangedEventArgs e)
                {
                    (source as HelpTopic).UpdateLinks((LinksCollection)e.NewValue);
                }
    
                private void UpdateLinks(LinksCollection NewCollection)
                {
                    links = NewCollection;
                    HelpTopics.ItemsSource = links;
                    HelpTopics.SelectedIndex = -1;
                }
            
            #endregion


    Tuesday, March 23, 2010 8:38 PM

Answers

  • Hi Christopher,

    You can add Link objects under the HelpTopics.Links property element directly without any change to your source code, because the collection type of the Link property has implemented the IList interface. For example:

    <local:HelpTopic Title="ARK Online Tutorials">
       <local:HelpTopic.Links>
           <local:NavigationLink LinkName="Overview" LinkAddress="http://www.yahoo.com" FontStyle="Italic" />
           <local:NavigationLink LinkName="Interface Navigation" LinkAddress="http://www.yahoo.com" />
           <local:NavigationLink LinkName="Modules" LinkAddress="http://www.yahoo.com" />
           <local:NavigationLink LinkName="Test Drive" LinkAddress="http://www.yahoo.com" />
       </local:HelpTopic.Links>
    </local:HelpTopic>

    Please try my suggestion to see if there's any problem.

    Sincerely,
    Linda Liu


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Linda Liu Tuesday, March 30, 2010 4:25 AM
    Monday, March 29, 2010 8:02 AM

All replies

  • There are a couple topics in the docs that might help.  Check these out.

    XAML and Custom Classes for WPF

    Collection-Type Dependency Properties

    Hope that helps,
    Margaret

    Tuesday, March 23, 2010 9:01 PM
  • Will definitely check those out.

    Thank you.

    Tuesday, March 23, 2010 9:06 PM
  • Hi Christopher,

    You can add Link objects under the HelpTopics.Links property element directly without any change to your source code, because the collection type of the Link property has implemented the IList interface. For example:

    <local:HelpTopic Title="ARK Online Tutorials">
       <local:HelpTopic.Links>
           <local:NavigationLink LinkName="Overview" LinkAddress="http://www.yahoo.com" FontStyle="Italic" />
           <local:NavigationLink LinkName="Interface Navigation" LinkAddress="http://www.yahoo.com" />
           <local:NavigationLink LinkName="Modules" LinkAddress="http://www.yahoo.com" />
           <local:NavigationLink LinkName="Test Drive" LinkAddress="http://www.yahoo.com" />
       </local:HelpTopic.Links>
    </local:HelpTopic>

    Please try my suggestion to see if there's any problem.

    Sincerely,
    Linda Liu


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Linda Liu Tuesday, March 30, 2010 4:25 AM
    Monday, March 29, 2010 8:02 AM