Reputation: 13
I have a Collection View like this in Maui:
<CollectionView Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2" WidthRequest="330" HorizontalOptions="Start" x:Name="CvVoting" HeightRequest="350" SelectionMode="Single" SelectionChanged="CvVoting_SelectionChanged" BackgroundColor="BurlyWood" VerticalScrollBarVisibility="Always">
<CollectionView.ItemTemplate>
<DataTemplate>
<StackLayout Orientation="Vertical" Padding="5" HeightRequest="200">
<Label x:Name="lbl_ID" FontSize="Small" Text="{Binding ID}" IsVisible="false"/>
<Label x:Name="lblQName" FontSize="20" Text="{Binding QuestionText}"/>
<RadioButton Content="Abstain" CheckedChanged="RadioButton_CheckedChanged" IsChecked="True"/>
<RadioButton Content="Yes" CheckedChanged="RadioButton_CheckedChanged"/>
<RadioButton Content="No" CheckedChanged="RadioButton_CheckedChanged"/>
<Button x:Name="btnCommit" Text="Commit" Clicked="btnCommit_Clicked" IsEnabled="false"/>
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
I need to access the label lbl_ID from my code-behind (in the Click-event of the button btnCommit)
private void btnCommit_Clicked(object sender, EventArgs e)
{
string ID = e.lbl_ID.Text;
}
Obviously the e.lbl_ID.Text is wrong - I need to know what to put there to access the text of the label.
Any help will be appreciated.
Upvotes: 1
Views: 1861
Reputation: 33993
I see you're trying to use a DataTemplate
. So here is the problem: a DataTemplate
is applied to each item in the backing collection. In your case, whatever ItemsSource
you set for your CvVoting
CollectionView
, for each item in that collection, a DataTemplate
will be shown.
With that in mind; how are you going to reference the right label? Potentially there are hundreds or thousands of Label
objects now with the same name, so this can't work.
What you want to do is use data-binding. And I see you're already doing that! Let's take that <Label x:Name="lbl_ID" FontSize="Small" Text="{Binding ID}" IsVisible="false"/>
as an example.
If you want to update the ID
here, you are going to have to update the ID
property of this item in your collection (the one assigned to ItemsSource
on your CollectionView
.
As already pointed out in the comments; you probably want to use the Command
property of your Button
instead of the Clicked
event. That way you can trigger logic in your view model as opposed to your view. The extra challenge here, is that you will now have to implement this command on your model if you set it up like this. While actually what you might want to do is add this command to your view model and with the CommandParameter
specify the ID
so that you know which object to update.
All of this is basically MVVM pattern. You do not want to reference any UI elements to capture or update values. You will always do that through the backing objects.
It's a bit of a hard concept to explain through just text and not knowing exactly what you want to achieve. I have some videos on my YouTube channel around databinding as well, maybe that helps. Besides this playlist there is a lot of other stuff that might help.
Upvotes: 4