I've been working on an application using a multi-level self-referencing heirarchy, and have come across a problem with using the RowLoaded event for any level other than the first level. Hopefully someone can give me a pointer on how to get this working.
My basic goal at the moment is to disable the expand button (+) next to items that do not have a heirarchy beneath them, I've recreated the sample from here in WPF, as a simple baseline. I've added a little to this to try to make it work as I need (including only having heirarchies related to some items), So I'll post my version of the code below. As I mentioned, I'm using the RowLoaded event to decide whether or not to make the row expandable, but this only works for the first level.
Is there a way I can use this RowLoaded event (or another one) to do the same for levels 2 - 10 (or however deep it goes)?
Thanks,
Russell
The MainWindow.xaml:
The MainWindow.xaml.cs:
My basic goal at the moment is to disable the expand button (+) next to items that do not have a heirarchy beneath them, I've recreated the sample from here in WPF, as a simple baseline. I've added a little to this to try to make it work as I need (including only having heirarchies related to some items), So I'll post my version of the code below. As I mentioned, I'm using the RowLoaded event to decide whether or not to make the row expandable, but this only works for the first level.
Is there a way I can use this RowLoaded event (or another one) to do the same for levels 2 - 10 (or however deep it goes)?
Thanks,
Russell
The MainWindow.xaml:
<Window x:Class="HeirarchicalGridExample.MainWindow" |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" |
xmlns:telerikGrid="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView" |
xmlns:telerikData="clr-namespace:Telerik.Windows.Data;assembly=Telerik.Windows.Data" |
Title="MainWindow" Height="350" Width="525"> |
<Grid> |
<telerikGrid:RadGridView x:Name="RadGridView1" ItemsSource="{Binding}" DataLoading="RadGridView1_DataLoading" RowLoaded="RadGridView1_RowLoaded" /> |
</Grid> |
</Window> |
The MainWindow.xaml.cs:
using System; |
using System.Collections.Generic; |
using System; |
using System.Collections.Generic; |
using System.Linq; |
using System.Windows; |
using System.Windows.Data; |
using Telerik.Windows.Controls.GridView; |
using Telerik.Windows.Controls; |
using Telerik.Windows.Data; |
namespace HeirarchicalGridExample |
{ |
/// <summary> |
/// Interaction logic for MainWindow.xaml |
/// </summary> |
public partial class MainWindow : Window |
{ |
public MainWindow() |
{ |
InitializeComponent(); |
DataContext = from i in Enumerable.Range(0, 5) |
select new MyObject() |
{ |
ID = i, |
Name = String.Format("Name{0}", i), |
Level = 1 |
}; |
} |
public class MyObject |
{ |
public int ID { get; set; } |
public string Name { get; set; } |
public int Level { get; set; } |
public IEnumerable<MyObject> Items |
{ |
get |
{ |
if (ID == 1 || ID == 3 || ID == 5 || ID == 7) |
{ |
return from i in Enumerable.Range(0, 10) |
select new MyObject() |
{ |
ID = i, |
Name = String.Format("{0}", Name + " - " + i.ToString()), |
Level = Level + 1 |
}; |
} |
else |
return Enumerable.Empty<MyObject>(); |
} |
} |
} |
private void RadGridView1_DataLoading(object sender, Telerik.Windows.Controls.GridView.GridViewDataLoadingEventArgs e) |
{ |
var grid = (GridViewDataControl)sender; |
var d = new GridViewTableDefinition(); |
d.Relation = new PropertyRelation("Items"); |
grid.TableDefinition.ChildTableDefinitions.Add(d); |
grid.AutoGenerateColumns = false; |
grid.Columns.Add(new GridViewDataColumn() { DataMemberBinding = new Binding("Level") }); |
grid.Columns.Add(new GridViewDataColumn() { DataMemberBinding = new Binding("ID") }); |
grid.Columns.Add(new GridViewDataColumn() { DataMemberBinding = new Binding("Name") }); |
} |
// this is only working for the top level of the grid |
private void RadGridView1_RowLoaded(object sender, RowLoadedEventArgs e) |
{ |
GridViewRow row = e.Row as GridViewRow; |
MyObject rowItem = e.DataElement as MyObject; |
if (row != null && rowItem != null) |
{ |
if (rowItem.Items.Count<MyObject>() > 0) |
row.IsExpandable = true; |
else |
row.IsExpandable = false; |
} |
} |
} |
} |