XAML 如何使用MVVM Toolkit库获取SelectedItem数据?

uwopmtnx  于 2022-12-16  发布在  其他
关注(0)|答案(1)|浏览(122)

我尝试在单击列表中的任何项目后触发事件或执行任何操作:

我正在使用CommunityToolkit.Mvvm库,但我不知道如何在单击或选择任何项目后获取其数据。例如,显示警报并在单击后显示其信息。
目前为止我有这个

<ListView ItemsSource="{Binding Almacenes}" HasUnevenRows="True" SelectedItem="{Binding SelectedItem}" >
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <Grid Padding="20">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>
                        <Image Grid.RowSpan="2"
                           Source="empresita.svg"
                           Aspect="AspectFill"
                           HeightRequest="60"
                           WidthRequest="60" />
                        <Label Grid.Column="1" Padding="15,0,0,0" 
                           Text="{Binding nombre}"
                           FontAttributes="Bold"
                            FontSize="16"/>
                        <Label Grid.Row="1" Padding="15,0,0,0"
                           Grid.Column="1"
                           Text="descripcion"
                           FontAttributes="Italic"
                           VerticalOptions="End" 
                               FontSize="16"/>
                    </Grid>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

后端:

[ObservableProperty]
        List<Almacenes> _almacenes;

        [ObservableProperty]
        Almacenes _selectedItem;

        public SeleccionAlmacenViewModel()
        {
            Almacenes = new List<Almacenes>()
            {
                new Almacenes()
                {
                    nombre = "Tomato"
                },
                new Almacenes()
                {
                    nombre = "Pizza"
                },
                new Almacenes()
                {
                    nombre = "Romaine"
                },
            };
        }

        // aqui se debe cargar la lista
        public SeleccionAlmacenViewModel(List<Almacenes> almacenes)
        {
            Almacenes = new List<Almacenes>();
            CargarAlmacenes(almacenes);
            
        }
        
        private void CargarAlmacenes(List<Almacenes> almacenes)
        {
            Almacenes = almacenes;
        }

没有错误,但是我不能使这个工作。我如何使用这个工具包得到选择的项目数据?就像我试图在一个方法中激发这个,因为我将在激发的方法中自己编码。
编辑:其实我不知道我在做什么,但我试过了,也不起作用:(

[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(GetAlmacenCommand))]
private Almacenes _selectedItem;

[RelayCommand]
public async Task GetAlmacen()
{
   await AppShell.Current.DisplayAlert("Error", "TESTING", "OK");
}
cu6pst1q

cu6pst1q1#

您可以尝试使用CommunityToolkit.Maui包将ListView的ItemSelected事件转换为命令。
在xaml中:

xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
<ListView ItemsSource="{Binding Almacenes}" HasUnevenRows="True" SelectedItem="{Binding SelectedItem Mode=TwoWay}" >
    .....
    <ListView.Behaviors>
          <toolkit:EventToCommandBehavior
                EventName="ItemSelected"
                Command="{Binding GetAlmacen}" />
    </ListView.Behaviors>
</ListView>

您可以从_selectedItem;中获取SelectedItem数据,因为您设置了SelectedItem="{Binding SelectedItem Mode=TwoWay}"

相关问题