On UWP everything is ok but on Android, SfTreeView throws null reference exception.
Model :
public class FoodSpecies : INotifyPropertyChanged
{
private string _speciesName;
private ObservableCollection<FoodSpecies> _species;
public ObservableCollection<FoodSpecies> Species
{
get { return _species; }
set
{
_species = value;
RaisedOnPropertyChanged("Species");
}
}
public string SpeciesName
{
get { return _speciesName; }
set
{
_speciesName = value;
RaisedOnPropertyChanged("SpeciesName");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisedOnPropertyChanged(string _PropertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(_PropertyName));
}
}
}
View :
<MasterDetailPage
x:Class="TreeViewTest.MainPage"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:treeView="clr-namespace:Syncfusion.XForms.TreeView;assembly=Syncfusion.SfTreeView.XForms"
xmlns:viewModel="clr-namespace:TreeViewTest.ViewModel;assembly=TreeViewTest">
<MasterDetailPage.BindingContext>
<viewModel:FoodSpeciesViewModel />
</MasterDetailPage.BindingContext>
<MasterDetailPage.MasterBehavior>
<MasterBehavior>Popover</MasterBehavior>
</MasterDetailPage.MasterBehavior>
<MasterDetailPage.Master>
<ContentPage Title="Tree View Test">
<StackLayout Spacing="0">
<Label
FontSize="Large"
HorizontalOptions="Center"
Text="Hello World !"
VerticalOptions="Center" />
</StackLayout>
</ContentPage>
</MasterDetailPage.Master>
<MasterDetailPage.Detail>
<ContentPage>
<Grid Margin="0" RowSpacing="0">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<treeView:SfTreeView
AutoExpandMode="AllNodesExpanded"
ChildPropertyName="Species"
ExpandActionTarget="Node"
ExpanderWidth="40"
Indentation="10"
ItemHeight="40"
ItemsSource="{Binding SpeciesType}">
<treeView:SfTreeView.ItemTemplate>
<DataTemplate>
<Grid Padding="5,0,0,0" BackgroundColor="Transparent">
<Label
FontSize="Medium"
Text="{Binding SpeciesName}"
VerticalTextAlignment="Center" />
</Grid>
</DataTemplate>
</treeView:SfTreeView.ItemTemplate>
</treeView:SfTreeView>
</Grid>
</ContentPage>
</MasterDetailPage.Detail>
</MasterDetailPage>
View Model :
public class FoodSpeciesViewModel
{
public ObservableCollection<FoodSpecies> SpeciesType { get; set; }
public FoodSpeciesViewModel()
{
var fruit = new FoodSpecies { SpeciesName = "Fruits" };
var oranges = new FoodSpecies { SpeciesName = "Oranges" };
var pineapple = new FoodSpecies { SpeciesName = "Pineapple" };
var apples = new FoodSpecies { SpeciesName = "Apples" };
var apple = new FoodSpecies { SpeciesName = "Apple" };
var macintosh = new FoodSpecies { SpeciesName = "Macintosh" };
var grannysmith = new FoodSpecies { SpeciesName = "Granny Smith" };
var fuji = new FoodSpecies { SpeciesName = "Fuji" };
fruit.Species = new ObservableCollection<FoodSpecies>
{
oranges,
pineapple,
apples,
};
apples.Species = new ObservableCollection<FoodSpecies> { apple, macintosh, grannysmith, fuji };
SpeciesType = new ObservableCollection<FoodSpecies> { fruit };
}
}