我如何在xamarin或其他语言中使用picker在模型中多选项目?

siv3szwd  于 2023-02-20  发布在  其他
关注(0)|答案(2)|浏览(182)

xaml.cs页面:

public partial class TalimatGorevEkle : ContentPage
    {
        TalimatGorevModelmobil Model;
        
        public TalimatGorevEkle() { 

            InitializeComponent();
            var kullanicilist = Methodlar.GorevliListeGetir(Sabitler.Token);
           
            
            KullaniciSec.ItemsSource = kullanicilist.GorevliListe;  //picker'da listelenecek kişiler

            
        }
    }

.xaml文件:

<?xml version="1.0" encoding="UTF-8" ?>
<ContentPage
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="OzelKalem.Views.ListeContentViews.TalimatGorevEkle">
    <ContentPage.Content>
        <ScrollView>
            <StackLayout>
              
               <Grid>
                   <StackLayout  Grid.Row="0" Padding="8">
                       <Label Text="Görev Başlığı:" TextColor="#FF7F24" FontSize="14"></Label>
                       <Entry x:Name="talimatAdi" Placeholder="Görev Başlığı Giriniz" HeightRequest="60" ></Entry>
                       <Label Text="Görev Adı:" TextColor="#FF7F24" FontSize="14" ></Label>
                       <Entry x:Name="talimatGörevAdi" Placeholder="Görev Adı Giriniz" HeightRequest="60" ></Entry>
                       <Label Text= "Görevin Son Tarihini Seçiniz:" TextColor="#FF7F24" FontSize="14"></Label>
                       <DatePicker Format="dd-MM-yyyy HH:mm" x:Name="gorevSonTarih" TextColor="Black" ></DatePicker>
                       <Picker x:Name="KullaniciSec" Title="Kullanıcı seçiniz." ItemsSource="{Binding GorevliListeModel}" ItemDisplayBinding="{Binding GorevliKullaniciAdi}"></Picker>
                       <Button x:Name="SaveCourseButton" Clicked="GorevEkle" Command="{Binding}" Text="Kaydet" BackgroundColor="LightGray" TextColor="#FF7F24" Margin="3" />
                       
                   </StackLayout>

               </Grid>
            </StackLayout>
        </ScrollView>
    </ContentPage.Content>
</ContentPage>

我在xamarin中有一个列表,我有它显示在选择器中。但我希望它是多个可选的。我怎么写它?我应该如何选择它作为复选框或在选择器上?

mbzjlibv

mbzjlibv1#

是的,对于多选项目,您可以使用CollectionView的“多选”来实现这一点。
而且你可以查看官方样品here
当然,您也可以将CheckBox添加到ListView的每一项来实现这一点。
这里有一个类似的线程,你可以在这里检查它:如何在xamarin表单中创建全选复选框。

piv4azn7

piv4azn72#

我已经使用了集合视图,但我需要在我的视图模型中将选定用户的列表设置为TaskList。实际上,我需要使用视图模型将此表单中输入的所有数据发送到服务。选定用户如何将列表分配给变量?
塔里马特戈里夫埃克尔. xaml.cs:

using System;
using System.Collections;
using System.Collections.Generic;
using OzelKalem.Models;
using Plugin.Toasts;
using Xamarin.Forms;

namespace OzelKalem.Views.ListeContentViews
{
    public partial class TalimatGorevEkle : ContentPage
    {
        TalimatGorevModelmobil Model;
        
        public TalimatGorevEkle() { 

            InitializeComponent();
            var kullanicilist = Methodlar.GorevliListeGetir(Sabitler.Token);
        

            CollectionView collectionView = new CollectionView
            {
                SelectionMode = SelectionMode.Multiple
            };
            collectionView.SetBinding(ItemsView.ItemsSourceProperty, "GorevliListeModel");
            collectionView.SelectionChanged += OnCollectionViewSelectionChanged;
            collectionlist.ItemsSource = kullanicilist.GorevliListe;
            collectionlist.ScrollTo(kullanicilist.GorevliListe);
            


        }
        void OnCollectionViewSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
          
        
            var previous = e.PreviousSelection;
            var current = e.CurrentSelection;
          
        }

        private void GorevEkle(object sender, EventArgs e)
        {

            var talimat = talimatAdi.Text;
            var talimatGorev = talimatGörevAdi.Text;
            var gorevSonTariih = gorevSonTarih.Date;
            GorevliListeModel items = collectionlist.SelectedItems as GorevliListeModel;
            

           


            }
        }

    }

TalimatGorevEkle.xaml:

<?xml version="1.0" encoding="UTF-8" ?>
<ContentPage
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="OzelKalem.Views.ListeContentViews.TalimatGorevEkle">
    <ContentPage.Content>
        <ScrollView>
            <StackLayout>
              
               <Grid>
                   <StackLayout  Grid.Row="0" Padding="8">
                       <Label Text="Görev Başlığı:" TextColor="#FF7F24" FontSize="14"></Label>
                       <Entry x:Name="talimatAdi" Placeholder="Görev Başlığı Giriniz" HeightRequest="60" ></Entry>
                       <Label Text="Görev Adı:" TextColor="#FF7F24" FontSize="14" ></Label>
                       <Entry x:Name="talimatGörevAdi" Placeholder="Görev Adı Giriniz" HeightRequest="60" ></Entry>
                       <Label Text= "Görevin Son Tarihini Seçiniz:" TextColor="#FF7F24" FontSize="14"></Label>
                       <DatePicker Format="dd-MM-yyyy HH:mm" x:Name="gorevSonTarih" TextColor="Black" ></DatePicker>

                       <!-- <Picker x:Name="KullaniciSec" Title="Kullanıcı seçiniz." ItemsSource="{Binding GorevliListeModel}" ItemDisplayBinding="{Binding GorevliKullaniciAdi}"></Picker>!-->
                       <Label Text="Kullanıcı Seçiniz:" TextColor="#FF7F24" FontSize="14" ></Label>
                       <CollectionView x:Name="collectionlist"
                    ItemsSource="{Binding GorevliListeModel}"
                    SelectionMode="Multiple"
                    SelectionChanged="OnCollectionViewSelectionChanged"
                    SelectedItems="{Binding GorevliKullaniciAdi}" Margin="10" HeightRequest="150">
             <CollectionView.ItemTemplate >
                 <DataTemplate >
                     <StackLayout >

                         <VisualStateManager.VisualStateGroups>
                    <VisualStateGroup Name="CommonStates">
                        <VisualState Name="Normal" >
                            <VisualState.Setters>
                                <Setter Property="BackgroundColor" Value="White" />
                            </VisualState.Setters>
                        </VisualState>
                        <VisualState Name="Selected">
                            <VisualState.Setters>
                                <Setter Property="BackgroundColor" Value="LightGray" />
                            </VisualState.Setters>
                        </VisualState>
                    </VisualStateGroup>
                </VisualStateManager.VisualStateGroups>

                         <Label Text="{Binding GorevliKullaniciAdi}" FontSize="14" Padding="5"></Label>
                     </StackLayout>

                 </DataTemplate>

             </CollectionView.ItemTemplate>
        
    </CollectionView>
                       <Button x:Name="SaveCourseButton" Clicked="GorevEkle" Command="{Binding}" Text="Kaydet" BackgroundColor="LightGray" TextColor="#FF7F24" Margin="3" />
                 
                   </StackLayout>

               </Grid>
            </StackLayout>
        </ScrollView>
    </ContentPage.Content>
</ContentPage>

视图模型:

public class TalimatGorevModelmobil
    {
        public string TalimatBaslik { get; set; }
        public string TalimatGorevAdi { get; set; }
        public int ID { get; set; }
        public int TalimatID { get; set; }
        public int Durum { get; set; }
        public DateTime EklenmeTarihi { get; set; }
        public int IslemKulID { get; set; }
        public List<GorevliListeModel> GorevliListe { get; set; }
        public DateTime SonTarih { get; set; }
        public int ServisBaglantiDID { get; set; }

    }
    public class GorevliListeModel
    {
        public string GorevliKullaniciAdi { get; set; }
        public string GorevliID { get; set; }
    }

相关问题