Skip to content

This example illustrates to bind the wpf treegrid columns from ViewModel in MVVM.

SyncfusionExamples/how-to-bind-columns-from-view-model-in-wpf-and-uwp-treegrid-in-mvvm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 

Repository files navigation

How to Bind Columns from ViewModel in WPF / UWP TreeGrid in MVVM?

This example illustrates to bind the columns from ViewModel in WPF TreeGrid and UWP TreeGrid (SfTreeGrid).

You can bind the SfTreeGrid.Columns property in ViewModel by having the binding property of Syncfusion.SfGrid.UI.Xaml.TreeGrid.Columns type. Thus, you can set binding to the SfTreeGrid.Columns property that provides DataContext of TreeGrid in ViewModel.

XAML:

<syncfusion:SfTreeGrid Name="treeGrid" 
                       Grid.Row="1" 
                       ChildPropertyName="ReportsTo"  
                       AutoExpandMode="AllNodesExpanded"
                       ShowRowHeader="True" 
                       Columns="{Binding SfGridColumns, Mode=TwoWay}"
                       AutoGenerateColumns="False"
                       ItemsSource="{Binding Employees}"
                       ParentPropertyName="ID"
                       SelfRelationRootValue="-1">
</syncfusion:SfTreeGrid>

Refer to the following code example in which the TreeGrid column is populated with some TreeGridTextColumn when creating the ViewModel instance.

C#:

public class ViewModel: NotificationObject
{
    #region Private Variables

    private ObservableCollection<EmployeeInfo> _employees;

    private TreeGridColumns sfGridColumns;

    #endregion

    public TreeGridColumns SfGridColumns
    {
        get { return sfGridColumns; }
        set
        { 
            this.sfGridColumns = value;
            RaisePropertyChanged("SfGridColumns");
        }
    }

    #region ctr

    public ViewModel()
    {
        this.Employees = GetEmployeesDetails();
        rowDataCommand = new RelayCommand(ChangeCanExecute);
        this.sfGridColumns = new TreeGridColumns();
        sfGridColumns.Add(new TreeGridTextColumn() { MappingName = "FirstName" });
        sfGridColumns.Add(new TreeGridTextColumn() { MappingName = "LastName" });
        sfGridColumns.Add(new TreeGridTextColumn() { MappingName = "Title" });
        sfGridColumns.Add(new TreeGridTextColumn() { MappingName = "Salary" });
    }
    #endregion
}

About

This example illustrates to bind the wpf treegrid columns from ViewModel in MVVM.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 5

Languages