Databinding Collection

2 posts, 1 answers
  1. Ralf
    Ralf avatar
    10 posts
    Member since:
    Nov 2010

    Posted 25 Nov 2010 Link to this post

    Hi!

    I´m trying to bind a collection of custom objects to a RadTreeListView. The object represents a `User`, who have a collection of different  rights and a collection of log entries.

    How can i define the section "Rights/Log Entries" within a HierarchicalDataTemplate? I´ve tried it based on your tutorial on binding collections.

    My Version of Telrik WPF is the current public release.

    The structure i would like to achieve should look like this:

    User 1
      Rights
         Right 1
         Right 2
       Log Entries
          Entry 1
          Entry 2
    User 2
       Rights
          Right 1
       Log Entries
    ...

    public sealed class User
    {
        public string Firstname
        {
            get;
            set;
        }
     
        public string Lastname
        {
            get;
            set;
        }
     
        public ObservableCollection<Right> Rights
        {
            get;
            set;
        }
     
        public ObservableCollection<LogEntry> LogEntries
        {
            get;
            set;
        }
     
        public User(string firstname, string lastname)
        {
            this.Firstname = firstname;
            this.Lastname = lastname;
            this.Rights = new ObservableCollection<Right>();
            this.LogEntries = new ObservableCollection<LogEntry>();
        }
    }
     
     
    public sealed class RightsCollection
    {
        public RightsCollection()
        {
            this.Name = "Rights";
            this.Rights = new ObservableCollection<Right>();
        }
         
        public string Name
        {
            get;
            set;
        }
         
        public ObservableCollection<Right> Rights
        {
            get;
            set;
        }
    }
     
    public sealed class Right
    {
        public string Name
        {
            get;
            set;
        }
     
        public string Description
        {
            get;
            set;
        }
     
        public bool Value
        {
            get;
            set;
        }
     
        public Right(string name)
        {
            this.Name = name;
            this.Description = String.Empty;
            this.Value = true;
        }
     
        public Right(string name, string description)
            : this(name)
        {
            this.Description = description;
        }
     
        public Right(string name, string description, bool value)
            : this(name, description)
        {
            this.Value = value;
        }
    }

    XAML:

    <UserControl.Resources>
        <local:Users x:Key="UserCollection" />      
         
        <HierarchicalDataTemplate x:Key="Right">
            <TextBlock Text="{Binding Name}" />           
        </HierarchicalDataTemplate>     
     
        <HierarchicalDataTemplate
            x:Key="Rights"
            ItemTemplate="{StaticResource Right}"
            ItemsSource="{Binding Rights}">
                         
            <TextBlock Text="{Binding Name}" />       
        </HierarchicalDataTemplate>
     
        <HierarchicalDataTemplate
            x:Key="Users"
            ItemTemplate="{StaticResource Rights}"
            ItemsSource="{Binding Rights}">
             
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Lastname}" />
                <TextBlock Text=", " />
                <TextBlock Text="{Binding Firstname}" />
            </StackPanel>
        </HierarchicalDataTemplate>
         
    </UserControl.Resources>
     
    <telerik:RadTreeView
        IsLineEnabled="True"
        ItemsSource="{Binding Source={StaticResource UserCollection}}"
        ItemTemplate="{StaticResource Users}" />
  2. Answer
    Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 30 Nov 2010 Link to this post

    Hello Ralf,

    In order to implement your scenario you will need to make sure that the Log Entries and Rights classes inherit one common class. I prepared a sample project for you illustrating one possible approach towards your scenario. Please have a look at it and let me know if it works for you.

    Sincerely yours,
    Petar Mladenov
    the Telerik team
    Browse the videos here>> to help you get started with RadControls for WPF
  3. UI for WPF is Visual Studio 2017 Ready
Back to Top