Hi friends!
I have a DataGrid that contains a list of product . when i click on a product i should open a popup windows that contains Product's details and i would modify the Price in it for example.
I implement a GlobalUsing.cs such :
global using System;
global using System.ComponentModel;
global using System.Collections.ObjectModel;
global using System.Diagnostics;
global using System.Text.Json;
global using Maui.DataGrid;
global using CommunityToolkit.Maui;
global using CommunityToolkit.Maui.Views;
global using MauiApp4.Views;
global using MauiApp4.Models;
global using MauiApp4.Services;
global using Microsoft.Maui.Controls;
namespace MauiApp4;
i write that code in the page ProductList.xaml
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MauiApp4.Views.ProductList"
xmlns:dg="clr-namespace:Maui.DataGrid;assembly=Maui.DataGrid"
Title="Liste des Produits">
<Grid RowDefinitions="70,*" BackgroundColor="Aquamarine">
<Button Text="Open Popup" Clicked="Button_Clicked"/>
<dg:DataGrid x:Name="productsCollection"
Grid.Row="1"
ItemsSource="{Binding Products}"
ItemSelected="DataGrid_ItemSelected"
SelectionEnabled="True"
SelectedItem="{Binding SelectedProduct}"
RowHeight="180" HeaderHeight="50"
PullToRefreshCommand="{Binding RefreshCommand}"
IsRefreshing="{Binding IsRefreshing}"
HeaderBackground="Red">
<!--Datagrid empty-->
<dg:DataGrid.NoDataView>
<Label BackgroundColor="AliceBlue" Text="Nothing to to see here 🙈" HorizontalTextAlignment="Center" HorizontalOptions="Center" VerticalOptions="Center" />
</dg:DataGrid.NoDataView>
<!--Remplir la Datagrid -->
<dg:DataGrid.Columns>
<dg:DataGridColumn Title="Image" Width="180" >
<dg:DataGridColumn.CellTemplate>
<DataTemplate>
<Image Aspect="AspectFill">
<Image.Source>
<UriImageSource
Uri="{Binding thumbnail}"
CacheValidity="00:12:00:00"
/>
</Image.Source>
</Image>
</DataTemplate>
</dg:DataGridColumn.CellTemplate>
</dg:DataGridColumn>
<dg:DataGridColumn Title="Code à Barre" PropertyName="id" SortingEnabled="True">
</dg:DataGridColumn>
<dg:DataGridColumn Title="Libellé" PropertyName="title" />
<dg:DataGridColumn Title="Prix" PropertyName="price" />
<dg:DataGridColumn Title="Catégorie" PropertyName="category" />
<dg:DataGridColumn Title="Marque" PropertyName="brand" />
</dg:DataGrid.Columns>
</dg:DataGrid>
</Grid>
</ContentPage>
in the page productList.cs i wrote:
namespace MauiApp4.Views;
public partial class ProductList : ContentPage
{
public ProductList()
{
InitializeComponent();
// Initializing the BindingContext with Products
BindingContext = new Models.AllProducts();
}
private void Button_Clicked(object sender, EventArgs e)
{
}
async void DataGrid_ItemSelected(object sender, SelectionChangedEventArgs e)
{
if (e.CurrentSelection.Count != 0)
{
Product product = e.CurrentSelection.FirstOrDefault() as Product;
var navigationParams = new Dictionary<string, object>
{
{ "product", product }
};
//Open popup window and pass the product selected
var popup = new ProductDetails();
Product result= await Shell.Current.CurrentPage.ShowPopupAsync(popup) as Product;
productsCollection.SelectedItem = null;
}
}
}
In the page ProductDetails.xaml defined as a popup page:
<?xml version="1.0" encoding="utf-8" ?>
<toolkit:Popup xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
x:Class="MauiApp4.Views.ProductDetails"
>
<ScrollView BackgroundColor="#eee" >
<VerticalStackLayout>
<CarouselView Loop="False"
ItemsSource="{Binding product.images}"
VerticalOptions="Start"
HeightRequest="300"
HorizontalScrollBarVisibility="Never" >
<CarouselView.ItemTemplate>
<DataTemplate>
<Image Source="{Binding}"
Aspect="AspectFit"
HeightRequest="300" />
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>
<VerticalStackLayout Margin="24, 10" Spacing="10" >
<Label Text="{Binding product.title}" FontSize="Title" FontFamily="OpenSansSemibold" />
<Label Text="{Binding product.description}" FontSize="Body" FontFamily="OpenSansRegular" /> -->
<Grid ColumnDefinitions="*,*" ColumnSpacing="30" Margin="0, 10" >
<HorizontalStackLayout>
<Label Text="💵 $" FontSize="30" VerticalTextAlignment="Center" FontFamily="OpenSansRegular" />
<!-- I can change the price of the product: -->
<Entry x:Name="newprice" Placeholder="Price" Text="{Binding product.price}" FontSize="40" FontFamily="OpenSansRegular" TextChanged="Entry_TextChanged" />
</HorizontalStackLayout>
</Grid>
<HorizontalStackLayout>
<Label Text="Brand: " FontSize="20" VerticalTextAlignment="Center" FontFamily="OpenSansRegular" />
<Label Text="{Binding product.brand}" FontSize="30" FontFamily="OpenSansRegular" />
</HorizontalStackLayout>
<HorizontalStackLayout>
<Label Text="Category: " FontSize="20" VerticalTextAlignment="Center" FontFamily="OpenSansRegular" />
<Label Text="{Binding product.category}" TextTransform="Uppercase" FontSize="30" FontFamily="OpenSansRegular" />
</HorizontalStackLayout>
<Button Text="Valider" Clicked="Button_Clicked" HorizontalOptions="CenterAndExpand"/>
</VerticalStackLayout>
</VerticalStackLayout>
</ScrollView>
</toolkit:Popup>
in the page ProductDetails.cs i wrote that code :
namespace MauiApp4.Views;
[QueryProperty(nameof(Product), "product")]
public partial class ProductDetails : Popup, IQueryAttributable, INotifyPropertyChanged
{
public Product product { get; private set; }
public void ApplyQueryAttributes(IDictionary<string, object> query)
{
product = query["product"] as Product;
OnPropertyChanged("product");
}
public ProductDetails()
{
InitializeComponent();
BindingContext = this;
Console.WriteLine("product inside details:" + product + "BINDINGCONTEXT::" + BindingContext);
}
// that line provide an error
private void Button_Clicked(object sender, EventArgs e) => Close( new ProductDetails() { Product.price = newprice.Text });