the picker is inside a collection and is rendered per row, the select ation works and triggers the command but it doesnt show the selected item when form is restored, it shows empty item instead of address proprety which is bound to selectedItem inside xaml.
here is the xaml code:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
xmlns:appCtrl="clr-namespace:WS.ORD.Apps.POS.Controls"
xmlns:appVM="clr-namespace:WS.ORD.Apps.POS.ViewModels"
xmlns:appModel="clr-namespace:WS.ORD.Apps.POS.Models"
x:Class="WS.ORD.Apps.POS.Views.PrinterSettingsPage"
x:Name="page">
<ContentPage.BindingContext>
<appVM:PrinterSettingsVM />
</ContentPage.BindingContext>
<Shell.BackButtonBehavior>
<BackButtonBehavior IsVisible="False" />
</Shell.BackButtonBehavior>
<ContentPage.Content>
<VerticalStackLayout BindableLayout.ItemsSource="{Binding Printers}" Spacing="30" Margin="100,50">
<BindableLayout.ItemTemplate>
<DataTemplate x:DataType="appModel:PrinterModel">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Text="{Binding Name}" VerticalOptions="Center" />
<Picker Grid.Row="0" Grid.Column="1"
ItemsSource="{Binding Source={RelativeSource AncestorType={x:Type appVM:PrinterSettingsVM}},Path=Devices}"
ItemDisplayBinding="{Binding .}"
HorizontalOptions="Fill"
VerticalOptions="Center"
SelectedItem="{Binding Address, Mode=TwoWay}"
Margin="0,0,0,10">
<Picker.Behaviors>
<toolkit:EventToCommandBehavior EventName="SelectedIndexChanged" Command="{Binding Source={x:Reference page}, Path=BindingContext.PrinterSelectCommand}" CommandParameter="{Binding .}" />
</Picker.Behaviors>
</Picker>
</Grid>
</DataTemplate>
</BindableLayout.ItemTemplate>
</VerticalStackLayout>
</ContentPage.Content>
</ContentPage>
here is codebehind:
public partial class PrinterSettingsPage : ContentPage
{
public PrinterSettingsPage()
{
InitializeComponent();
}
}
here is the viewmodel:
namespace WS.ORD.Apps.POS.ViewModels
{
internal class PrinterSettingsVM : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public ICommand PrinterSelectCommand { get; private set; }
public PrinterSettingsVM()
{
this.Devices[0] = "device1";
this.Devices[1] = "device2";
this.Devices[2] = "device3";
this.Printers = new List<PrinterModel>()
{
new PrinterModel { Id=Guid.NewGuid(), Name="Printer 2", Address="device1" },
new PrinterModel { Id=Guid.NewGuid(), Name="Printer 2", Address="device2" }
};
}
private string[] _devices = new string[3];
public string[] Devices
{
get { return this._devices; }
set
{
this._devices = value;
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(nameof(this.Devices)));
}
}
}
private IEnumerable<PrinterModel> _printers;
public IEnumerable<PrinterModel> Printers
{
get { return this._printers; }
set
{
this._printers = value;
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(nameof(this.Printers)));
}
}
}
}
}
finally the model:
namespace WS.ORD.Apps.POS.Models
{
public class PrinterModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public Guid Id { get; set; }
public string Name { get; set; }
private string _address;
public string Address
{
get { return this._address; }
set
{
this._address = value;
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(nameof(this.Address)));
}
}
}
}
}
You can try to add a variable
SelectedAddressto save the selected item and initialize it in the constructor of your viewmodel.Please refer to the following code:
Usage example:
Update:
You can put the
DevicesandSelectedAddressinside ofPrinterModel.1.you can add property
DevicesandSelectedAddressinto modelPrinterModel.cs2.And initialize property
DevicesandSelectedAddressin the constructor ofPrinterSettingsVM.cs,just as follows:3.modify the data bind as follows: