// Xaml Code
<sfgrid:SfDataGrid x:Name="dataGrid"
ItemsSource="{Binding OrdersInfo}"
AutoGenerateColumns="False"
SelectionMode="Single"
ColumnSizer="Star">
<sfgrid:SfDataGrid.Columns >
<sfgrid:GridTemplateColumn MappingName="LastName" >
<sfgrid:GridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid Padding="5,5,5,5">
<Button Text="click here" Clicked="Button_Clicked"/>
</Grid>
</DataTemplate>
</sfgrid:GridTemplateColumn.CellTemplate>
</sfgrid:GridTemplateColumn>
<sfgrid:GridTextColumn MappingName="FirstName" />
<sfgrid:GridTextColumn MappingName="LastName" />
<sfgrid:GridTextColumn MappingName="FirstName"/>
</sfgrid:SfDataGrid.Columns>
</sfgrid:SfDataGrid>
//Code behind
private void Button_Clicked(object sender, EventArgs e)
{
dataGrid.SelectedItem = (sender as Button).BindingContext;
}
|
//XAML Code
<sfgrid:SfDataGrid x:Name="dataGrid"
ItemsSource="{Binding OrdersInfo}"
AutoGenerateColumns="False"
SelectionMode="Single"
SelectedItem="{Binding DataGridSelectedItem}"
ColumnSizer="Star">
<sfgrid:SfDataGrid.Columns >
<sfgrid:GridTemplateColumn MappingName="LastName" >
<sfgrid:GridTemplateColumn.CellTemplate>
<DataTemplate>
<Button WidthRequest="100" Text="Click" Command="{Binding Source={x:Reference dataGrid}, Path=BindingContext.TapCommand}" CommandParameter="{Binding .}" />
</DataTemplate>
</sfgrid:GridTemplateColumn.CellTemplate>
</sfgrid:GridTemplateColumn>
<sfgrid:GridTextColumn MappingName="FirstName" />
<sfgrid:GridTextColumn MappingName="LastName" />
<sfgrid:GridTextColumn MappingName="FirstName"/>
</sfgrid:SfDataGrid.Columns>
</sfgrid:SfDataGrid>
// ViewModel Codes
private object dataGridSelectedItem;
public ICommand TapCommand { private set; get; }
public object DataGridSelectedItem
{
get
{
return dataGridSelectedItem;
}
set
{
this.dataGridSelectedItem = value;
RaisePropertyChanged("DataGridSelectedItem");
}
}
public ViewModel()
{
order = new OrderInfoRepository();
SetRowstoGenerate(5);
TapCommand = new Command(OnTapped);
}
public void OnTapped(object selectedItem)
{
this.DataGridSelectedItem = (selectedItem as OrderInfo);
} |
Hello.
I when I try to work with the fields of the button selected they are unabled.
Something like that:
Can you give some advice ?
Thaks
Enrique
Hi Enrique,
Based on the information provided, the issue you are experiencing appears to be related to type conversion. Within the clicked event, you are accessing the binding context of the button. The binding context received will be of object type. Therefore, it is necessary to convert it to the model class for further use elsewhere. We have prepared a simple sample that fulfills your requirement. We have attached a code snippet and sample for your reference.
private void Button_Clicked(object sender, EventArgs e) { dataGrid.SelectedItem = (sender as Button).BindingContext; var empInfo = dataGrid.SelectedItem as Employee; string Name = empInfo.EmployeeID + empInfo.Name; } |
Regards,
Nirmalkumar