This is a migrated thread and some comments may be shown as answers.

Databinding Collection

1 Answer 62 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Ralf
Top achievements
Rank 1
Ralf asked on 25 Nov 2010, 12:04 PM
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}" />

1 Answer, 1 is accepted

Sort by
0
Accepted
Petar Mladenov
Telerik team
answered on 30 Nov 2010, 06:44 PM
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
Tags
TreeView
Asked by
Ralf
Top achievements
Rank 1
Answers by
Petar Mladenov
Telerik team
Share this question
or