【问题标题】:pass an id of itemselected of list to the button xamarin将列表中 itemselected 的 id 传递给按钮 xamarin
【发布时间】:2020-06-10 13:53:03
【问题描述】:

我想将列表中所选项目的 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;

        // 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】:

    使用senderBindingContext

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

    【讨论】:

      猜你喜欢
      • 2020-11-16
      • 2016-07-29
      • 2020-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-16
      • 2021-12-07
      • 1970-01-01
      相关资源
      最近更新 更多