<sfgrid:SfDataGrid x:Name="dataGrid" ItemsSource="{Binding DataList}" AllowLoadMore="True" LoadMoreCommand="{Binding LoadMoreCommand}" AllowPullToRefresh="True" PullToRefreshCommand="{Binding RefreshCommand}" AllowResizingColumn="True" AutoGenerateColumns="false" AllowSorting="True" AllowMultiSorting="True" AllowEditing="True" SelectionMode="Single" VerticalOverScrollMode="None"> <sfgrid:SfDataGrid.Columns> <sfgrid:GridTextColumn MappingName="Name" HeaderText="Name"></sfgrid:GridTextColumn> <sfgrid:GridPickerColumn MappingName="GlobalId" ItemsSource="{Binding ExpenseGroups}" DisplayMemberPath="Name" ValueMemberPath="Id" HeaderFontAttribute="Bold" HeaderText="Group"> </sfgrid:GridPickerColumn> <sfgrid:GridSwitchColumn MappingName="ActiveFlag" HeaderText="Active?"></sfgrid:GridSwitchColumn> </sfgrid:SfDataGrid.Columns> </sfgrid:SfDataGrid>
This is the ContentView code
public CVGridExpenseCategories() { InitializeComponent(); ThemeManager.ThemeName = Themes.Light; ViewModel = new ExpenseCategoryViewModel(); ViewModel.LoadMoreCommand.Execute(null); BindingContext = ViewModel; } public ExpenseCategoryViewModel ViewModel { get; set; }When it runs, it throws a NullReferenceException. Why is the binding NOT WORKING???And why do all your examples use this inline viewmodel declaration?<sample:SampleView.BindingContext> <local:GettingStartedViewModel x:Name="viewModel" /> </sample:SampleView.BindingContext>Is there something wrong with binding to a viewmodel like this?<sfgrid:SfDataGrid x:Name="dataGrid" ItemsSource="{Binding DataList}" AllowLoadMore="True"Also, why is this not working?<sfgrid:GridPickerColumn MappingName="GlobalId" ItemsSource="{Binding ExpenseGroups}" DisplayMemberPath="Name" ValueMemberPath="Id" HeaderFontAttribute="Bold" HeaderText="Group"> </sfgrid:GridPickerColumn>
How do you reference the BindingContext for the GridPickerColumn from the viewmodel that is not declared inline?
Is this the only way of binding data to the GridPickerColumn?BindingContext="{x:Reference viewModel}"This has been really frustrating. Also, I am not sure your Nuget packages add all the required dependencies to the Platform (Android,iOS) projects. Please look into this.Please I need working answers.Thanks.
ViewModel = new ExpenseCategoryViewModel(); ViewModel.LoadMoreCommand.Execute(null); BindingContext = ViewModel;
ViewModel.LoadMoreCommand.Execute(null);ViewModel = new ExpenseCategoryViewModel(); ViewModel.LoadMoreCommand.Execute(null); BindingContext = ViewModel;
|
<ContentPage.BindingContext>
<local:ViewModel x:Name="viewModel" />
</ContentPage.BindingContext>
<ContentPage.Content>
<sfgrid:SfDataGrid x:Name="sfGrid"
AutoGenerateColumns="False"
ItemsSource="{Binding OrdersInfo}"
AllowResizingColumn="True"
ColumnSizer="Star"
AllowLoadMore="True"
LoadMoreCommand="{Binding LoadMore}"
IsBusy="{Binding Busy}"
>
<sfgrid:SfDataGrid.Columns>
<sfgrid:GridTextColumn MappingName="OrderID" />
<sfgrid:GridTextColumn MappingName="EmployeeID" />
<sfgrid:GridTextColumn MappingName="CustomerID" />
<sfgrid:GridTextColumn MappingName="ShipCountry" HeaderText="Country"/>
</sfgrid:SfDataGrid.Columns>
</sfgrid:SfDataGrid>
</ContentPage.Content>
//ViewModel.cs
public class ViewModel : NotificationObject
{
internal OrderInfoRepository order;
public ICommand LoadMore { get; set; }
private bool busy;
public bool Busy
{
get
{
return busy;
}
set
{
busy = value;
RaisePropertyChanged("Busy");
}
}
public ViewModel()
{
order = new OrderInfoRepository();
SetRowstoGenerate(50);
LoadMore = new CustomLoadMoreCommand(this);
}
#region ItemsSource
private ObservableCollection<OrderInfo> ordersInfo;
public ObservableCollection<OrderInfo> OrdersInfo
{
get { return ordersInfo; }
set
{
this.ordersInfo = value;
RaiseCollectionChanged("OrdersInfo");
}
}
#endregion
#region ItemSource Generator
public void SetRowstoGenerate(int count)
{
OrdersInfo = order.GetOrderDetails(count);
}
#endregion
}
//CustomLoadMoreCommand.cs
public class CustomLoadMoreCommand : ICommand
{
public event EventHandler CanExecuteChanged;
public ViewModel ViewModel { get; set; }
public CustomLoadMoreCommand(ViewModel viewModel)
{
ViewModel = viewModel;
}
public bool CanExecute(object parameter)
{
return true;
}
public async void Execute(object parameter)
{
// You can write your set of codes that needs to be executed
this.ViewModel.Busy = true;
await Task.Delay(new TimeSpan(0, 0, 5));
for (int i = 0; i < 20; i++)
this.ViewModel.OrdersInfo.Add(this.ViewModel.order.GenerateOrder(this.ViewModel.OrdersInfo.Count + 1));
this.ViewModel.Busy = false;
}
} |