I found this GitHub post that shows a handy Xamarin DataGrid. However, I want to take it a step further and add a checkbox as the left most column, so that I can then on button click capture all the ID's from the grid that have been selected.
Is this achievable in Xamarin.Forms
and C#
?
edit
So after much googling I discovered it would be much easier to use a "toggle" and I have this code for my XAML. My issue is how do I bind my database fields to the binding for the labels?
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Test.Pages.TestApprove" >
<ContentPage.Content>
<StackLayout>
<Label Text="The users below are Requesting Access:"></Label>
<Grid Padding="5,0" RowSpacing="1" ColumnSpacing="1">
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="auto" />
</Grid.ColumnDefinitions>
<Switch Grid.Row="0" Grid.Column="0" VerticalOptions="Center"
HorizontalOptions="Center" BackgroundColor="Brown" />
<Label Text="{Binding fname}" Grid.Row="0" Grid.Column="1"
Grid.ColumnSpan="1" Margin="1"
BackgroundColor="Blue" IsEnabled="false"/>
<Entry Text="{Binding lname}" Grid.Row="0" Grid.Column="2"
Grid.ColumnSpan="1" Margin="1" IsEnabled="false"
FontSize="Small" BackgroundColor="Purple" />
<Entry Text="{Binding company}" Grid.Row="0" Grid.Column="3"
Grid.ColumnSpan="1" Margin="1"
FontSize="Small" BackgroundColor="Green" />
<Entry Text="{Binding Phone}" Grid.Row="0" Grid.Column="4"
Grid.ColumnSpan="1" Margin="1"
FontSize="Small" BackgroundColor="Orange" />
</Grid>
<Button Command="{Binding ApproveUserCommand}" Text="Approve User" TextColor="White"
FontAttributes="Bold" FontSize="Large" HorizontalOptions="FillAndExpand"
BackgroundColor="#088da5" />
</StackLayout>
</ContentPage.Content>
</ContentPage>
And of course I want to dynamically generate the number of rows that the select query returns...so if there are 10 users requesting access, I should be 10 rows with each users data. Am I going about this the correct way/. How do I bind the data?
Xaml:
<ContentPage x:Name="self">
<dg:DataGrid ItemsSource="{Binding Users}">
<dg:DataGrid.Columns>
<dg:DataGridColumn Title="Authorized" PropertyName="IsAuthorized" Width="100">
<dg:DataGridColumn.CellTemplate>
<DataTemplate>
<Switch IsToggled="{Binding .}" HorizontalOptions="Center" VerticalOptions="Center"/>
</DataTemplate>
</dg:DataGridColumn.CellTemplate>
</dg:DataGridColumn>
<dg:DataGridColumn Title="Name" PropertyName="Name" Width="2*"/>
<dg:DataGridColumn Title="Action" PropertyName="Id" PropertyName="Streak" Width="0.7*" IsSortable="False">
<dg:DataGridColumn.CellTemplate>
<DataTemplate>
<!-- Here is the trick: command binds the ViewModel's method, since we needed the Id, we've added the id as column's binding context -->
<Button Text="Authorize" Command="{Binding AuthorizeCommand,source={x:Reference self}}" CommandParameter="{Binding .}"/>
</DataTemplate>
</dg:DataGridColumn.CellTemplate>
</dg:DataGridColumn>
</dg:DataGrid.Columns>
</dg:DataGrid>
</ContentPage>
ViewModel:
class MainViewModel
{
public MainViewModel()
{
AuthorizeCommand = new Command<string>(CmdAuthorize);
}
public ICommand AuthorizeCommand{get;set;}
public List<User> Users{get;set;}
CmdAuthorize(string id)
{
var user = Users.First(x=>x.Id == id);
user.IsAuthorized = true;
}
}
Model:
class User: INotifyPropertyChanged
{
bool _isAuthorized;
public event PropertyChangedEventHandler PropertyChanged;
public string Name{get;set;}
public string Id{get;set;}
public bool IsAuthorized
{
get=>_isAuthorized;
set
{
_isAuthorized =value;
PropertyChanged?.Invoke(nameof(IsAuthorized));
}
}
}
ps: Do not forget setting BindingContext
of the page
contentPage.BindingContext= new MainViewModel()