具有联接表和选取器循环的结构表

Jiale Xue - MSFT 43,046 信誉分 Microsoft 供应商
2024-05-31T08:33:27.7333333+00:00

我有 2 个关于表和选取器的问题:

  1. 如何像这张图片一样使用连接表来构建此表?

70201-h149.png 2. 假设我有 3 名学生。如果实现,如何编写循环命令将选择器值获取到 Table?picker valuecolectionviewloopAbsensi

我在 中创建了一个如下数据集合。xaml

<CollectionView Margin="-15"   
                x:Name="AbsenList"   
                SelectionMode="Single"   
                VerticalOptions="CenterAndExpand">  
    <CollectionView.ItemTemplate>  
        <DataTemplate>  
            <StackLayout Orientation="Horizontal">  
                <Image Source="{Binding StudentImage}"/>  
                <Label Text="{Binding Name}"/>  
                <Picker Title="Absen" SelectedItem="{Binding Checklist}">  
                    <Picker.ItemsSource>  
                        <x:Array Type="{x:Type x:String}">  
                            <x:String>A</x:String>  
                            <x:String>B</x:String>  
                            <x:String>C</x:String>  
                            <x:String>D</x:String>  
                        </x:Array>  
                    </Picker.ItemsSource>  
                </Picker>  
            </StackLayout>  
        </DataTemplate>  
    </CollectionView.ItemTemplate>  
</CollectionView>  
<Button Text="Save" Clicked="SaveToDatabase"/>  

如果有人回答这个问题,谢谢:)

Note:此问题总结整理于: Structure Table with Joined Table and Picker Loop

Xamarin
Xamarin
用于使用 .NET 和 C# 构建 Android 和 iOS 应用的 Microsoft 开源应用平台。
14 个问题
C#
C#
一种面向对象的类型安全的编程语言,它起源于 C 语言系列,包括对面向组件的编程的支持。
170 个问题
0 个注释 无注释
{count} 票

接受的答案
  1. Hui Liu-MSFT 47,336 信誉分 Microsoft 供应商
    2024-05-31T09:32:34.38+00:00

    欢迎来到我们的 Microsoft 问答平台!

    如果实现,如何编写循环命令将选择器值获取到 Absensi 表?

    您可以在物料模型类中创建一个属性来存储所选数据,也可以将所选数据传递给事件中的属性。SelectedIndexChanged

    您可以参考以下代码:

        <CollectionView ItemsSource="{Binding Monkeys}" VerticalOptions="Start">  
            <CollectionView.ItemTemplate>  
                <DataTemplate>  
                    <Grid Padding="3">  
                        <Grid.RowDefinitions>  
                            <RowDefinition Height="Auto" />  
                            <RowDefinition Height="Auto" />  
                        </Grid.RowDefinitions>  
                        <Grid.ColumnDefinitions>  
                            <ColumnDefinition Width="1*" />  
                            <ColumnDefinition Width="1*" />  
                            <ColumnDefinition Width="1*" />  
                        </Grid.ColumnDefinitions>  
                        <Image Grid.RowSpan="2"   
                               Source="{Binding ImageUrl}"   
                               Aspect="AspectFill"  
                               HeightRequest="60"   
                               WidthRequest="60" />  
                        <Label Grid.Column="1"   
                               Text="{Binding Name}"   
                               FontAttributes="Bold" />  
                        <Label Grid.Row="1"  
                               Grid.Column="1"   
                               Text="{Binding Location}"  
                               FontAttributes="Italic"   
                               VerticalOptions="End" />  
    
                      <Picker Title="Absen" Grid.RowSpan="2" Grid.Column="2" SelectedItem="{Binding Checklist}"  SelectedIndexChanged="Picker_SelectedIndexChanged">  
                            <Picker.ItemsSource>  
                                <x:Array Type="{x:Type x:String}">  
                                    <x:String>A</x:String>  
                                    <x:String>B</x:String>  
                                    <x:String>C</x:String>  
                                    <x:String>D</x:String>  
                                </x:Array>  
                            </Picker.ItemsSource>  
                        </Picker>  
                    </Grid>  
                </DataTemplate>  
            </CollectionView.ItemTemplate>  
        </CollectionView>  
    

    2.在项目模型中添加字段(我的项目是Checklist Monkey)

    public class Monkey: INotifyPropertyChanged  
    
    {
    private string _Name;
    public string Name
    {
    set { SetProperty(ref _Name, value); }
    get { return _Name; }
    } // add field Checklist
    private string _checklist;
    public string Checklist {
    set { SetProperty(ref _checklist, value); }
    get { return _checklist; }
    }
    
     public string Location { get; set; }  
     public string Details { get; set; }  
     public string ImageUrl { get; set; }  
    
    
     bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)  
     {  
         if (Object.Equals(storage, value))  
             return false;  
    
         storage = value;  
         OnPropertyChanged(propertyName);  
         return true;  
     }  
    
     protected void OnPropertyChanged([CallerMemberName] string propertyName = null)  
     {  
         PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));  
     }  
    
     public event PropertyChangedEventHandler PropertyChanged;  
    
    }
    

    3.在公共分部类yourpage.xaml.cs

    
    VerticalListPage : ContentPage
    {
    public VerticalListPage()
    {
    InitializeComponent();
    BindingContext = new MonkeysViewModel();
    }
     private void Picker_SelectedIndexChanged(object sender, System.EventArgs e)  
     {  
         var picker = sender as Picker;  
    
         var index = picker.SelectedIndex;  
         var list = picker.ItemsSource;  
    
         var model = picker.BindingContext as Monkey;  
         model.Checklist = (string)list[index];  
     }  
    
    
    }
    

    --- 如果回复有帮助,请点击“接受答案”并点赞。

    注意:如果您想接收此线程的相关电子邮件通知,请按照我们文档中的步骤启用电子邮件通知。

    1 个人认为此答案很有帮助。
    0 个注释 无注释

0 个其他答案

排序依据: 非常有帮助