将列表中所选项目的ID传递给按钮xamarin

问题描述 投票:0回答:1

我想将列表中所选项目的id(参数)传递给按钮,但我不知道该怎么做。知道的话,有一列有一个选择器(2个项目:修改和删除),所以我想当我单击“ supprimer”(意味着删除)时,我想调出所选项目的ID。 顺便说一下,该列表使用http rest API获取数据。(顺便说一句,我可以在添加列时在列表中显示ID,但是我不需要它)请我需要帮助

xaml

 <ListView x:Name="viewRapport" ItemsSource="{Binding Rapports}" 
                      SeparatorVisibility="None" SelectionMode="None" ItemTapped="viewRapport_ItemTapped">
              <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <StackLayout Orientation="Vertical">

                                <Frame  BorderColor="white" Padding="0">
                                    <!--  BackgroundColor="AliceBlue" CornerRadius="12" BorderColor="Blue" Padding="0" -->
                                    <Grid>
                                        <!--Padding="20" Margin="5" BackgroundColor="Wheat"-->
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="3*"/>
                                            <ColumnDefinition Width="2.5*"/>
                                            <ColumnDefinition Width="2.5*"/>
                                            <ColumnDefinition Width="2.5*"/>
                                            <!--/****/
                                            <ColumnDefinition Width="0"/>
                                                /****/-->
                                                <ColumnDefinition Width="*"/>
                                        </Grid.ColumnDefinitions>
                                            <Label Grid.Column="0" HorizontalTextAlignment="Start" LineBreakMode="WordWrap" Text="{Binding Name}"/>
                                            <Label Grid.Column="1" Text="{Binding Amount}" LineBreakMode="WordWrap" IsVisible="{Binding is}" x:Name="q"/>
                                            <Label Grid.Column="2" LineBreakMode="WordWrap" Text="{Binding AmountReimbursed}"/>
                                            <!--/****/
                                            <Label Grid.Column="3" x:Name="idSelected" IsVisible="False" LineBreakMode="WordWrap" Text="{Binding Id}"/>
                                           /****/-->
                                            <Label Grid.Column="3" TextColor="Red" x:Name="status"  LineBreakMode="WordWrap" Text="{Binding Status}">
                                                <!--
                                                PropertyChanged="status_PropertyChanged" 
                                                <Label Grid.Column="3" PropertyChanged="status_PropertyChanged" TextColor="{Binding MyTextColor, Converter={StaticResource StringToColorConverter}}"-->
                                            </Label>
                                            <Picker Grid.Column="4" x:Name="ListRap"  Title="..."  SelectedIndexChanged="rapp_SelectedIndexChanged" >
                                            <Picker.ItemsSource>
                                                <x:Array Type="{x:Type x:String}">
                                                    <x:String>Modifier</x:String>
                                                    <x:String>Supprimer</x:String>
                                                </x:Array>
                                            </Picker.ItemsSource>
                                        </Picker>

                                        <!--  <ImageButton Source="dot.jpg" Grid.Column="2" HorizontalOptions="End" HeightRequest="20" Clicked="ImageButton_Clicked"/>
                                   <Image.GestureRecognizers>
                                        <TapGestureRecognizer Command="{Binding ThreeDottedIconPopup}"/>
                                    </Image.GestureRecognizers>
                                </Image> -->
                                    </Grid>
                                </Frame>

                                <Grid>

                                    <Grid.RowDefinitions>
                                        <RowDefinition/>
                                    </Grid.RowDefinitions>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition/>
                                        <ColumnDefinition/>
                                        <ColumnDefinition/>
                                    </Grid.ColumnDefinitions>

xaml.cs:

  public ListRapport()
    {
        InitializeComponent();
        GetExpenseReport();
        RootModel viewModel = new RootModel();
        viewModel.Name = AppContext.AppContext.Instence?.CurrentUser.Name;
        BindingContext = viewModel;

enter image description here

        // var titre = r.Title;
        //viewRapport.ItemsSource= titre;
    }


    public async void GetExpenseReport()
    {

        HttpClient httpClient = new HttpClient();
        httpClient.BaseAddress = new Uri("http://192.168.1.20:3000/api/adepApi/GetExpenseReports");
        httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        HttpResponseMessage response = await httpClient.GetAsync("http://192.168.1.20:3000/api/adepApi/GetExpenseReports");
        string content = "";
        if (response != null)
        {
            content = await response.Content.ReadAsStringAsync();
            ResponseData EL = JsonConvert.DeserializeObject<ResponseData>(content);
            viewRapport.ItemsSource = EL.Data.expenseRLists;

        }

我的模特:和睦:

 public class Rapport
{
    //public string Titre { get; set; }
    public bool IsVisible { get; set; }

    public int Id { get; set; }
    public  int? UserId { get; set; }
    public string Name { get; set; }
    public int? Duration { get; set; }
    public string Description { get; set; }
    public decimal? Amount { get; set; }
    public decimal? AmountReimbursed { get; set; }
    public decimal? AmountReal { get; set; }
xaml xamarin button xamarin.forms parameter-passing
1个回答
0
投票

使用BindingContextsender

protected void rapp_SelectedIndexChanged(object Sender, EventArgs a)
{
  var picker = sender as Picker;
  var item = (Rapport)picker.BindingContext;
  var id = item.Id;

  ...
}
© www.soinside.com 2019 - 2024. All rights reserved.