问题
In my Xamarin, I have two CardView
(https://github.com/tiger4589/Xamarin.Forms-CardView) within separate Grid.Column
.
What I want to do is select a CardView (one at a time), which enables the Button.
How could I acheive this?
.xml code for 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 code for Button
<Button
x:Name="FormButton"
IsEnabled="False"
TextColor="#4DABFE"
Text="Submit">
</Button>
UPDATE ON CollectionView
<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>
回答1:
your collectionview would look like this
<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>
Now you need to create a model Eg. MyDataModel which would have property as follow
class MyDataModel
{
public int ItemId{ get; set; }
public string Title { get; set; }
public string ItemColor { get; set; }
}
now you need to init the list in your .cs file also u can do this view viewmodel but in your case you are using .cs so it would look something like this in your constructor
var myList = new List<MyDataModel>();
Loop your XmlData List
eg. XmlDataList.ForEach((item)=>{ myList.Add(new MyDataModel{Title = item.properyinxml
})});
MyCollection.ItemSource = myList;
来源:https://stackoverflow.com/questions/64875081/select-the-cardview-in-xamarin