Chọn CardView trong Xamarin

Nov 17 2020

Trong Xamarin của tôi, tôi có hai CardView(https://github.com/tiger4589/Xamarin.Forms-CardView) trong phạm vi riêng biệt Grid.Column.

Những gì tôi muốn làm là chọn một CardView (mỗi lần một cái) để bật Nút.

Làm sao tôi có thể hiểu được điều này?

.xml mã cho CardView

<Grid Grid.Column="0">
    <cardView:CardView>
        <cardView:CardView.CardViewContent>
            <StackLayout>
                <Label 
                    Text="PIN"
                    FontSize="18"
                    HorizontalTextAlignment="Center">
                </Label>
            </StackLayout>
        </cardView:CardView.CardViewContent>
    </cardView:CardView>
</Grid>

<Grid Grid.Column="1">
    <cardView:CardView>
        <cardView:CardView.CardViewContent>
            <StackLayout>
                <Label 
                    Text="QR Scan"
                    FontSize="18"
                    HorizontalTextAlignment="Center">
                </Label>
            </StackLayout>
        </cardView:CardView.CardViewContent>
    </cardView:CardView>
</Grid>

.xml mã cho Nút

<Button
    x:Name="FormButton"
    IsEnabled="False"
    TextColor="#4DABFE"
    Text="Submit">
</Button>

CẬP NHẬT VỀ Chế độ xem bộ sưu tập

<StackLayout>
    <Frame >
        <CollectionView ItemsSource="{Binding .}"
            SelectionMode="Single">
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <StackLayout Orientation="Horizontal">
                        <Label Text="Tokyo"
                               FontSize="20"
                               TextColor="Orange"
                   VerticalOptions="Center" />
                    </StackLayout>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>
    </Frame>
</StackLayout>

Trả lời

1 RonakShethia Nov 17 2020 at 14:48

chế độ xem bộ sưu tập của bạn sẽ giống như thế này

 <CollectionView x:Name="MyCollection"
        SelectionMode="Single">
        <CollectionView.ItemTemplate>
            <DataTemplate>
                <StackLayout Orientation="Horizontal">
                    <Label Text="{Binding Title}"
                           FontSize="20"
                           TextColor="{Binding ItemColor}"
               VerticalOptions="Center" />
                </StackLayout>
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>

Bây giờ bạn cần tạo một mô hình Ví dụ. MyDataModel sẽ có thuộc tính như sau

class MyDataModel
        {
         public int ItemId{ get; set; }
         public string Title { get; set; }
         public string ItemColor { get; set; }
        }

bây giờ bạn cần init danh sách trong tệp .cs của bạn. Bạn cũng có thể thực hiện chế độ xem này, nhưng trong trường hợp của bạn, bạn đang sử dụng .cs, vì vậy nó sẽ trông giống như thế này trong hàm tạo của bạn

var myList = new List<MyDataModel>();

Loop your XmlData List
eg. XmlDataList.ForEach((item)=>{ myList.Add(new MyDataModel{Title = item.properyinxml 
 })});

MyCollection.ItemSource = myList;