I'm trying to use a GridTreeControl
, bound to a collection, populated with Model
objects:
public class Attachment
{
public int ID { get; set; }
public string Name { get; set; }
public int ParentID { get; set; }
}
My View
looks like:
<StackPanel>
<syncfusion:GridTreeControl>
<syncfusion:GridTreeControl.DataContext>
<local:ViewModel/>
</syncfusion:GridTreeControl.DataContext>
<i:Interaction.Behaviors>
<b:RequestTreeItemsBehavior/>
</i:Interaction.Behaviors>
<syncfusion:GridTreeControl.Columns>
<syncfusion:GridTreeColumn MappingName="Name"/>
</syncfusion:GridTreeControl.Columns>
</syncfusion:GridTreeControl>
<Button Content="add" Command="{Binding AddCommand}"/>
</StackPanel>
I also use behavior to request tree items from collection correctly:
public class RequestTreeItemsBehavior : Behavior<GridTreeControl>
{
ViewModel viewModel;
protected override void OnAttached()
{
base.OnAttached();
viewModel = this.AssociatedObject.DataContext as ViewModel;
this.AssociatedObject.RequestTreeItems += new GridTreeRequestTreeItemsHandler(AssociatedObject_RequestTreeItems);
}
void AssociatedObject_RequestTreeItems(object sender, GridTreeRequestTreeItemsEventArgs args)
{
if (args.ParentItem == null)
{
//get the root list - get all attachments who have no parent
args.ChildList = viewModel.AttachmentsList.Where(a => a.ParentID == -1); //get all attachments whose parentID is -1 (no parent)
}
else
{
Attachment att = args.ParentItem as Attachment;
if (att != null)
{
//get all attachments that report to the parent attachment
args.ChildList = viewModel.GetChildren(att.ID);
}
}
}
protected override void OnDetaching()
{
base.OnDetaching();
this.AssociatedObject.RequestTreeItems -= new GridTreeRequestTreeItemsHandler(AssociatedObject_RequestTreeItems);
}
}
And I populate my collection in ViewModel
constructor:
public class ViewModel : NotificationObject
{
private ICommand addCommand;
public ICommand AddCommand
{
get
{
if (addCommand == null)
addCommand = new RelayCommand(param => OnAddCommand());
return addCommand;
}
}
private void OnAddCommand()
{
AttachmentsList.Add(new Attachment { ID = 10, Name = "2SUBSUB", ParentID = 4 });
RaisePropertyChanged("AttachmentsList");
}
public ObservableCollection<Attachment> AttachmentsList { get; set; }
public ViewModel()
{
AttachmentsList = new ObservableCollection<Attachment>();
Attachment Root = new Attachment { ID = 1, Name = "ROOT", ParentID = -1 };
Attachment Sub1 = new Attachment { ID = 3, Name = "1SUB", ParentID = 1 };
Attachment Sub2 = new Attachment { ID = 4, Name = "2SUB", ParentID = 1 };
Attachment SubSub1 = new Attachment { ID = 5, Name = "1SUBSUB", ParentID = 4 };
AttachmentsList.Add(Root);;
AttachmentsList.Add(Sub1);
AttachmentsList.Add(Sub2);
AttachmentsList.Add(SubSub1);
}
public int FindID(int id) {...}
public IEnumerable<Attachment> GetChildren(int parentID) {...}
}
At the beginning, everything works pretty fine, but after I try to use AddCommand
, my new object is added to ObservableCollection, but it's not displayed in UI, even after invocation of RaisePropertyChanged()
. What should I add to my Behaviour
to deal with it? Or should I bind to my collection with some different way?